This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Socket Server Backend

By: xxmbabanexx on Mar 15th, 2013  |  syntax: Python  |  size: 2.39 KB  |  views: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import thread
  2. import socket
  3. import sys
  4.  
  5.  
  6. HOST = socket.gethostname()
  7. print HOST
  8.  
  9. PORT = 2468
  10. welcome_msg = 0
  11.  
  12.  
  13. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
  14. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  15. print "Socket Created"
  16.  
  17. try:
  18.     s.bind((HOST, PORT))
  19. except socket.error, msg:
  20.     print "Bind failed. Error Code : " + str(msg[0]) + " Message " + str(msg[1])
  21.     sys.exit()
  22. print "Socket Bind Complete"
  23. """
  24. So by specifying 10, it means that if 10
  25. connections are already waiting to be processed,
  26. then the 11th connection request shall be rejected.
  27. This will be more clear after checking socket_accept.
  28. """
  29. s.listen(10)
  30. print "Socket now listening"
  31.  
  32.  
  33. def open_welcome():
  34.     welcome_txt = open("welcome.txt", "r")
  35.     welcome_msg = welcome_txt.read()
  36.     print welcome_msg
  37.     return welcome_msg
  38.  
  39. def open_msg():
  40.     send_msg = open("send_able.txt", "r")
  41.     data = send_msg.read()
  42.     send_msg.close()
  43.     send_msg = open("send_able.txt", "w+")
  44.     send_msg.write("")
  45.     send_msg.close()
  46.  
  47. def old_msg(data):
  48.     while True:
  49.         oldmsg = open("old_msg.txt", "a+")
  50.         try:
  51.             oldmsg.write("%s \n" % (data))
  52.         except TypeError:
  53.             print "SERVER MSG >>> NO MESSAGE SENT"
  54.         oldmsg.close()
  55.  
  56. #Function for handling connections
  57. def clientthread(connection, welcome_msg):
  58.  
  59.     #Sending message to connected client
  60.     #This only takes strings (words
  61.  
  62.     connection.send(open_welcome())
  63.  
  64.     #loop so that function does not terminate and the thread does not end
  65.     while True:
  66.  
  67.         #Receiving from client
  68.         data = connection.recv(1024)
  69.         if not data:
  70.             break
  71.         if data:
  72.             connection.sendall(data)
  73.             old_msg(data)
  74.         print data
  75.        
  76.     connection.close()
  77.  
  78. def main_server():
  79.     while True:
  80.        
  81.         #Wait to accept a connection - blocking call
  82.         connection, addr = s.accept()
  83.         #display client information (IP address)
  84.         print 'Connected with ' + addr[0] + ':' + str(addr[1])
  85.  
  86.         #Start new thread takees 1st argument as a function name to be run, second
  87.         #is the tuple of arguments to the function
  88.  
  89.         thread.start_new_thread(clientthread (connection, welcome_msg))
  90.  
  91. def main():
  92.     open_welcome()
  93.     open_msg()
  94.     main_server()
  95.  
  96. main()
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.    
  115.  
  116. s.close()
clone this paste RAW Paste Data