Advertisement
xxmbabanexx

simple socket server 2

Apr 5th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 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 threading import * #Creates threading capabilities.
  18.  
  19.  
  20. #////////////////////////////////////////////////
  21. """
  22. This section of the code is used to define the socket
  23. server and its respective functions. These functions are
  24. used by the GUI part to send messages to clients
  25. """
  26. #/////////////////////////////////////////////////
  27.  
  28. BUFFER = 1024 #Buffer for recv of msg
  29.  
  30. import socket #Needed for connectivity
  31. import sys #needed for exit
  32.  
  33. HOST = 'localhost'   # Loopback
  34. PORT = 1234 # Random port that works
  35.  
  36. addr = (HOST, PORT) #I create the address based on the HOST and PORT variables
  37.  
  38. print addr #so that the user can see the addr
  39.  
  40. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#creates a socket object 's'
  41. print 'Socket created' #prints msg
  42.  
  43. try: #tries to bind to port
  44.     s.bind((HOST, PORT))
  45. except socket.error , msg: #if fail, do this
  46.     print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
  47.     sys.exit()
  48.      
  49. print 'Socket bind complete' #prints msg
  50.  
  51. s.listen(10) #listen for connections
  52. print 'Socket now listening'
  53.  
  54. def SERVER_LOOP(s):
  55.     """Function for accepting connections to server"""
  56.     global conn
  57.     conn, addr = s.accept()
  58.     print "Connected with", addr
  59.    
  60.        
  61.        
  62. def RECEIVE(conn, BUFFER):
  63.     """Receiving function"""
  64.     while True:
  65.         try:
  66.             #Sending message to connected client
  67.             #This only takes strings (words
  68.             conn.recv(BUFFER)
  69.  
  70.         except Exception:
  71.             import traceback
  72.             print traceback.format_exc()
  73.        
  74. def send_msg(conn, message):
  75.  
  76.     """Send Messages"""
  77.     #Send some data to the remote server
  78.     my_message = message.get("0.0", END)
  79.  
  80.  
  81.      #set the whole string
  82.     conn.sendall(my_message)
  83.  
  84. def START_SERVER(addr):
  85.     """Rudimentary client to start server. Helps for testing"""
  86.     client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  87.     client.connect(("localhost", 1234))
  88.     client.send("sup")
  89.    
  90.  
  91. Thread(target = START_SERVER, args = (addr,)).start() #Start client thread
  92.  
  93.  
  94. #////////////////////////////////////////////////
  95. """
  96. This code 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
  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() #start gui mainloop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement