Advertisement
xxmbabanexx

Simple GUI Server

Apr 1st, 2013
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | None | 0 0
  1. #////////////////////////////////////////////////
  2. """
  3. Here I imported lots of different modules.
  4. """
  5. #/////////////////////////////////////////////////
  6.  
  7.  
  8.  
  9. from Tkinter import * #Used to create the GUI
  10.  
  11. from ScrolledText import ScrolledText #Used to create the Scrolled Text
  12.  
  13. import socket #Used to create the server
  14.  
  15. import sys #Used for exiting in case of issue
  16.  
  17. from thread import * #Creates threading capabilities.
  18.  
  19.  
  20. #////////////////////////////////////////////////
  21. """
  22. This class (CHANGE NEEDED HERE) is used to define the socket.
  23. It creates the server, and has a send_cmd method
  24. which is called upon by the Tkinter class
  25. Application
  26. """
  27. #/////////////////////////////////////////////////
  28.  
  29. BUFFER = 1024
  30.  
  31. def start_up():
  32.     HOST = socket.gethostname()
  33.     IP = socket.gethostbyname(HOST)
  34.     PORT = 2468
  35.  
  36.     print "HOST: %s" % (HOST)
  37.     print "HOST IP: %s" % (IP)
  38.     print "PORT: %s" % (PORT)
  39.     print "\n"
  40.     print "-------------------------"
  41.     print "\n"
  42.     global s
  43.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
  44.     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  45.     print "Socket Created"
  46.  
  47.     try:
  48.         s.bind((HOST, PORT))
  49.     except socket.error, msg:
  50.         print "Bind failed. Error Code : " + str(msg[0]) + " Message " + str(msg[1])
  51.         sys.exit()
  52.     print "Socket Bind Complete"
  53.     """
  54.    So by specifying 10, it means that if 10
  55.    connections are already waiting to be processed,
  56.    then the 11th connection request shall be rejected.
  57.    This will be more clear after checking socket_accept.
  58.    """
  59.     s.listen(10)
  60.     print "Socket now listening"
  61.  
  62.  
  63. def mainloop(s):
  64.     while True:
  65.         channel, addr = s.accept()
  66.         print "Connected with", addr
  67. def RECEIVE(s):
  68.     try:
  69.         #Sending message to connected client
  70.         #This only takes strings (words
  71.         s.recv(BUFFER)
  72.  
  73.     except Exception:
  74.         import traceback
  75.         print traceback.format_exc()
  76. def send_msg(s,message):
  77.     #Send some data to the remote server
  78.     my_message = message.get("0.0", END)
  79.  
  80.  
  81.      #set the whole string
  82.     s.sendall(my_message)
  83.     myHistory = open("history.txt", "a+")
  84.     myHistory.write(my_message)
  85.  
  86. def main():
  87.     start_up()
  88.     mainloop(s)
  89.  
  90. main()
  91.  
  92.  
  93.  
  94. #////////////////////////////////////////////////
  95. """
  96. This class is used to hold the GUI stuff.
  97. It uses Tkinter and multiple frames to accomplish this task.
  98. It calls on commands from the Socket_Server class
  99. """
  100. #/////////////////////////////////////////////////
  101.  
  102.  
  103.  
  104. #Setup Stuff
  105. root = Tk()
  106. root.title("HoweIM Server GUI")
  107. #make my screen dimensions work
  108. w = 500
  109. h = 1000
  110.  
  111. root.geometry("500x1000")
  112.  
  113.  
  114. #Create the Main-Frame
  115. Hold_Frame = Frame(root)
  116. Hold_Frame.grid()
  117.  
  118.  
  119.  
  120. #Past Frame            
  121.  
  122. pastFrame = Frame(Hold_Frame, width = 100, height = 300)
  123. pastFrame.grid()
  124. pastText = ScrolledText(pastFrame, height = 30,
  125.                         width = 50,
  126. #This line (state = DISABLED) means that the user cannot edit this textbox
  127.                         state = DISABLED,
  128. #This line (relief = GROOVE, bd = 5) tells Tkinter to make a groove aroudn the box, and the width of the border
  129. #Is now 5
  130.                         relief = GROOVE, bd = 5)
  131.  
  132. pastText.grid(row = 1)
  133.  
  134.  
  135. #Message Frame
  136.  
  137. messageFrame = Frame(Hold_Frame, width = 100, height = 100)
  138. messageFrame.grid()
  139.  
  140. space = Label(messageFrame)
  141. space.grid()
  142.  
  143. message = Text(messageFrame, height = 10, width = 50,
  144.                     relief = GROOVE, borderwidth = 5,)
  145. message.grid()
  146.  
  147. send = Button (messageFrame, text = "Send",
  148.                     command = lambda: send_msg(s,message))
  149. send.grid(row = 1, column = 1, sticky = S)
  150.  
  151.  
  152. #Side Frame
  153.  
  154. sideFrame = Frame(Hold_Frame)
  155.  
  156.  
  157.  
  158.  
  159.  
  160. root.mainloop()
  161.    
  162.                        
  163.                
  164.  
  165. #The functions that you find from now on are commands for Buttons
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement