Guest User

Newps

a guest
Mar 20th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #-----------------------------[Section 2: Communications]
  2.  
  3. # Defining Variables
  4. # Outgoing Data Buffer
  5. out_buffer = []
  6.  
  7. # Incoming Data Function
  8. def incoming():
  9. rlist,wlist,xlist = select.select([s], out_buffer, [])
  10. print("listening")
  11. while 1:
  12. for i in rlist:
  13. data = i.recv(1024)
  14. if data:
  15. reply_decode = data.decode()
  16. reply_processed = json.loads(reply_decode)
  17.  
  18. # Message Decoding
  19.  
  20. Reply_Trans = []
  21. y = 0
  22. Reply_Limit = len(reply_processed)
  23. while y < Reply_Limit:
  24. try:
  25. Reply_Trans = Reply_Trans + [R_Dictionary[reply_processed[y]]]
  26. except ValueError:
  27. tkinter.messagebox.showinfo("Internal Error","Section 3: IDF ValueError")
  28.  
  29. y += 1
  30. Reply_Combine = "".join(str(p) for p in Reply_Trans)
  31. print("Reply_Combine")
  32. chatbox.insert(INSERT,"Client: "+Reply_Combine+ "\n")
  33. if numlines > 15:
  34. chatbox.delete("1.0",END)
  35. chatbox.insert(INSERT,"Console: Limit Reached, Chat Wiped" + "\n")
  36. INPUT_BOX.set("")
  37.  
  38. # Outgoing Data Function
  39. def outgoing():
  40. global out_buffer
  41. try:
  42. user_input = USER_ENTRY.get()
  43. chatbox.insert(INSERT,"You: "+USER_ENTRY.get()+ "\n")
  44. user_lower = user_input.lower()
  45. Mess_Trans = [] # Holds Translated message
  46. x = 0
  47. Count_Limit = len(user_lower)
  48. while x < Count_Limit:
  49. try:
  50. Mess_Trans = Mess_Trans + [T_Dictionary[user_lower[x]]]
  51. except ValueError:
  52. print("Internal Error Exception")
  53. tkinter.messagebox.showinfo("Internal Error","Dictionary Failed To Encrypt")
  54. x += 1
  55. # Serliazing List Before Exporting
  56. Mess_Seri = json.dumps(Mess_Trans)
  57.  
  58. # Exporting Message
  59.  
  60. if user_input:
  61. out_buffer += [Mess_Seri.encode()]
  62. # for i in wlist:
  63. s.send(out_buffer[0])
  64. out_buffer = []
  65. print("Sending..")
  66. numlines = len(chatbox.get("1.0",END).splitlines())
  67. if numlines > 15:
  68. chatbox.delete("1.0",END)
  69. chatbox.insert(INSERT,"Console: Limit Reached, Chat Wiped" + "\n")
  70. INPUT_BOX.set("")
  71.  
  72. except IOError:
  73. print("\n\t\aIOError Exception")
  74. tkinter.messagebox.showinfo("Internal Error","Section 3: IOError on send attempt")
  75. input("\n\nPress Enter To Exit")
  76.  
  77. # ----------------------------[ Section 2, GUI Chatbox ]
  78. # Setting Variables
  79. global textlimiter
  80. textlimiter = 0
  81. # Defining Functions
  82. # Chat Control and Command
  83. def CONVERT():
  84. numlines = len(chatbox.get("1.0",END).splitlines())
  85. if numlines > 15:
  86. chatbox.delete("1.0",END)
  87. chatbox.insert(INSERT,"Console: Limit Reached, Chat Wiped" + "\n")
  88. INPUT_BOX.set("")
  89. def chatexit():
  90. time.sleep(0.5)
  91. sys.exit()
  92.  
  93. # Starting Incoming.
  94. def bicommand():
  95. TI = 0
  96. outgoing()
  97.  
  98. if TI == 0:
  99. thread_in = threading.Thread(target=incoming, args=())
  100. thread_in.start()
  101.  
  102. #thread_in.start() # this causes the thread to run
  103. #thread_in.join doesn't seem to affect the freeze....
  104. #thread_in.join() # this waits until the thread has completed
  105.  
  106.  
  107.  
  108. # Chatbox Window
  109. chat = Tk()
  110. chat.title("Net Send Client [0.5]")
  111. chat.geometry('550x500+200+200')
  112. #
  113. title = StringVar()
  114. title.set("Net Send Chat\n Type in box to send.")
  115. title_widget = Label(chat,textvariable = title, height = 4)
  116. title_widget.pack()
  117. #
  118. INPUT_BOX = StringVar()
  119. USER_ENTRY = Entry(chat,textvariable = INPUT_BOX)
  120. USER_ENTRY.pack(side=TOP, padx = 10, pady = 20)
  121. #
  122. send_button = Button(chat, text="Send",width = 20, command = bicommand)
  123. send_button.pack(padx = 10, pady = 10)
  124. #
  125. chatbox = Text(width = 60, height = 15, relief = SUNKEN)
  126. left = Frame(chatbox)
  127. right = Frame(chatbox)
  128. s_start = Scrollbar(right)
  129. s_start.pack(side=RIGHT)
  130. #
  131. exit_button = Button(chat, text="Close & Exit",width = 20, command = chatexit)
  132. exit_button.pack(side='bottom',padx = 15, pady = 14)
  133. #
  134. chatbox.grid(row = 0, column = 0, columnspan = 3)
  135. chatbox.pack()
  136. #
  137. chat.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment