Advertisement
xxmbabanexx

Server GUI Alpha Function

Mar 30th, 2013
235
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None
  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. def start_up():
  30.     HOST = socket.gethostname()
  31.     IP = socket.gethostbyname(HOST)
  32.     PORT = 2468
  33.  
  34.     print "HOST: %s" % (HOST)
  35.     print "HOST IP: %s" % (IP)
  36.     print "PORT: %s" % (PORT)
  37.     print "\n"
  38.     print "-------------------------"
  39.     print "\n"
  40.     global s
  41.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
  42.     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  43.     print "Socket Created"
  44.  
  45.     try:
  46.         s.bind((HOST, PORT))
  47.     except socket.error, msg:
  48.         print "Bind failed. Error Code : " + str(msg[0]) + " Message " + str(msg[1])
  49.         sys.exit()
  50.     print "Socket Bind Complete"
  51.     """
  52.    So by specifying 10, it means that if 10
  53.    connections are already waiting to be processed,
  54.    then the 11th connection request shall be rejected.
  55.    This will be more clear after checking socket_accept.
  56.    """
  57.     s.listen(10)
  58.     print "Socket now listening"
  59.  
  60.  
  61. def accept():
  62.     #Wait to accept a connection - blocking call
  63.     addr = s.accept()
  64.     #display client information (IP address)
  65.     print 'Connected'
  66.  
  67.     #Start new thread takees 1st argument as a function name to be run, second
  68.     #is the tuple of arguments to the function
  69.  
  70.     start_new_thread(clientthread ,(s,))
  71.  
  72.     s.close()
  73.  
  74. #Function for handling connections. This will be used to create threads
  75. def clientthread(s):
  76.  
  77.     #Sending message to connected client
  78.     #This only takes strings (words
  79.     s.send("Welcome to the server. Type something and hit enter\n")
  80.  
  81.     #loop so that function does not terminate and the thread does not end
  82.     while True:
  83.  
  84.         #Receiving from client
  85.         data = s.recv(1024)
  86.         if not data:
  87.             break
  88.         s.sendall(data)
  89.         print data
  90.     s.close()
  91. def send_msg(s,message):
  92.     #Send some data to the remote server
  93.     my_message = message.get("0.0", END)
  94.  
  95.  
  96.      #set the whole string
  97.     s.sendall(my_message)
  98.     myHistory = open("history.txt", "a+")
  99.     myHistory.write(my_message)
  100.  
  101. def main():
  102.     start_up()
  103.     accept()
  104.  
  105. def sendable():
  106.     accept()
  107.     send_msg(s)
  108.  
  109.  
  110.  
  111. #////////////////////////////////////////////////
  112. """
  113. This class is used to hold the GUI stuff.
  114. It uses Tkinter and multiple frames to accomplish this task.
  115. It calls on commands from the Socket_Server class
  116. """
  117. #/////////////////////////////////////////////////
  118.  
  119.  
  120.  
  121. #Setup Stuff
  122. root = Tk()
  123. root.title("HoweIM Server GUI")
  124. #make my screen dimensions work
  125. w = 500
  126. h = 1000
  127.  
  128. root.geometry("500x1000")
  129.  
  130.  
  131. #Create the Main-Frame
  132. Hold_Frame = Frame(root)
  133. Hold_Frame.grid()
  134.  
  135.  
  136.  
  137.            
  138.  
  139. pastFrame = Frame(Hold_Frame, width = 100, height = 300)
  140. pastFrame.grid()
  141. pastText = ScrolledText(pastFrame, height = 30,
  142.                         width = 50,
  143. #This line (state = DISABLED) means that the user cannot edit this textbox
  144.                         state = DISABLED,
  145. #This line (relief = GROOVE, bd = 5) tells Tkinter to make a groove aroudn the box, and the width of the border
  146. #Is now 5
  147.                         relief = GROOVE, bd = 5)
  148.  
  149. pastText.grid(row = 1)
  150.  
  151.        
  152.  
  153. messageFrame = Frame(Hold_Frame, width = 100, height = 100)
  154. messageFrame.grid()
  155.  
  156. space = Label(messageFrame)
  157. space.grid()
  158.  
  159. message = Text(messageFrame, height = 10, width = 50,
  160.                     relief = GROOVE, borderwidth = 5,)
  161. message.grid()
  162.  
  163. send = Button (messageFrame, text = "Send",
  164.                     command = lambda: send_msg(s,message))
  165. send.grid(row = 1, column = 1, sticky = S)
  166.  
  167.  
  168.  
  169.  
  170.        
  171. main()
  172. root.mainloop()
  173.    
  174.                        
  175.                
  176.  
  177. #The functions that you find from now on are commands for Buttons
Advertisement
RAW Paste Data Copied
Advertisement