Advertisement
xxmbabanexx

Console Server

Apr 1st, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 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. import socket
  32. import sys
  33.  
  34. HOST = ''   # Symbolic name meaning all available interfaces
  35. PORT = 8888 # Arbitrary non-privileged port
  36.  
  37. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  38. print 'Socket created'
  39.  
  40. try:
  41.     s.bind((HOST, PORT))
  42. except socket.error , msg:
  43.     print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
  44.     sys.exit()
  45.      
  46. print 'Socket bind complete'
  47.  
  48. s.listen(10)
  49. print 'Socket now listening'
  50.  
  51.  
  52.  
  53. def serverloop(s):
  54.     while True:
  55.         global conn
  56.         conn, addr = s.accept()
  57.         print "Connected with", addr
  58.        
  59. def RECEIVE(BUFFER):
  60.     try:
  61.         #Sending message to connected client
  62.         #This only takes strings (words
  63.         conn.recv(BUFFER)
  64.  
  65.     except Exception:
  66.         import traceback
  67.         print traceback.format_exc()
  68.        
  69. def send_msg(conn):
  70.     #Send some data to the remote server
  71.     my_message = raw_input(">>>")
  72.  
  73.  
  74.      #set the whole string
  75.     conn.sendall(my_message)
  76.  
  77. def main():
  78.     start_new_thread(serverloop, (s, ))
  79.     RECEIVE(conn, BUFFER)
  80.     send_msg(conn)
  81.  
  82. main()
  83.  
  84.  
  85.  
  86. #////////////////////////////////////////////////
  87. """
  88. This class is used to hold the GUI stuff.
  89. It uses Tkinter and multiple frames to accomplish this task.
  90. It calls on commands from the Socket_Server class
  91. """
  92. #/////////////////////////////////////////////////
  93.  
  94.  
  95. """
  96. #Setup Stuff
  97. root = Tk()
  98. root.title("HoweIM Server GUI")
  99. #make my screen dimensions work
  100. w = 500
  101. h = 1000
  102.  
  103. root.geometry("500x1000")
  104.  
  105.  
  106. #Create the Main-Frame
  107. Hold_Frame = Frame(root)
  108. Hold_Frame.grid()
  109.  
  110.  
  111.  
  112. #Past Frame            
  113.  
  114. pastFrame = Frame(Hold_Frame, width = 100, height = 300)
  115. pastFrame.grid()
  116. pastText = ScrolledText(pastFrame, height = 30,
  117.                        width = 50,
  118. #This line (state = DISABLED) means that the user cannot edit this textbox
  119.                        state = DISABLED,
  120. #This line (relief = GROOVE, bd = 5) tells Tkinter to make a groove aroudn the box, and the width of the border
  121. #Is now 5
  122.                        relief = GROOVE, bd = 5)
  123.  
  124. pastText.grid(row = 1)
  125.  
  126.  
  127. #Message Frame
  128.  
  129. messageFrame = Frame(Hold_Frame, width = 100, height = 100)
  130. messageFrame.grid()
  131.  
  132. space = Label(messageFrame)
  133. space.grid()
  134.  
  135. message = Text(messageFrame, height = 10, width = 50,
  136.                    relief = GROOVE, borderwidth = 5,)
  137. message.grid()
  138.  
  139. send = Button (messageFrame, text = "Send",
  140.                    command = lambda: send_msg(s,message))
  141. send.grid(row = 1, column = 1, sticky = S)
  142.  
  143.  
  144. #Side Frame
  145.  
  146. sideFrame = Frame(Hold_Frame)
  147.  
  148.  
  149.  
  150.  
  151.  
  152. root.mainloop()
  153.    
  154.                      
  155.                
  156. """
  157. #The functions that you find from now on are commands for Buttons
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement