Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. import sys
  2. import socket
  3. import select
  4. import MySQLdb
  5. HOST = ''
  6. SOCKET_LIST = []
  7. RECV_BUFFER = 4096
  8. PORT = 9009
  9.  
  10. def chat_server():
  11.  
  12.     server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13.     server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  14.     server_socket.bind((HOST, PORT))
  15.     server_socket.listen(10)
  16.  
  17.     # add server socket object to the list of readable connections
  18.     SOCKET_LIST.append(server_socket)
  19.  
  20.     print "Chat server started on port " + str(PORT)
  21.  
  22.     while 1:
  23.  
  24.         # get the list sockets which are ready to be read through select
  25.         # 4th arg, time_out  = 0 : poll and never block
  26.         ready_to_read,ready_to_write,in_error = select.select(SOCKET_LIST,[],[],0)
  27.      
  28.         for sock in ready_to_read:
  29.             # a new connection request recieved
  30.             if sock == server_socket:
  31.                 sockfd, addr = server_socket.accept()
  32.                 SOCKET_LIST.append(sockfd)
  33.                 print "Client (%s, %s) connected" % addr
  34.                  
  35.                 broadcast(server_socket, sockfd, "[%s:%s] entered our chatting room\n" % addr)
  36.              
  37.             # a message from a client, not a new connection
  38.             else:
  39.                 # process data recieved from client,
  40.                 try:
  41.                     # receiving data from the socket.
  42.                     data = sock.recv(RECV_BUFFER)
  43.                     if data:
  44.                         # there is something in the socket
  45.                         broadcast(server_socket, sock, "\r" + '[' + str(sock.getpeername()) + '] ' + data)  
  46.                     else:
  47.                         # remove the socket that's broken    
  48.                         if sock in SOCKET_LIST:
  49.                             SOCKET_LIST.remove(sock)
  50.  
  51.                         # at this stage, no data means probably the connection has been broken
  52.                         broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
  53.  
  54.                 # exception
  55.                 except:
  56.                     broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
  57.                     continue
  58.  
  59.     server_socket.close()
  60.    
  61. # broadcast chat messages to all connected clients
  62. def broadcast (server_socket, sock, message):
  63.     for socket in SOCKET_LIST:
  64.         # send the message only to peer
  65.         if socket != server_socket and socket != sock :
  66.             try :
  67.                 socket.send(message)
  68.             except :
  69.                 # broken socket connection
  70.                 socket.close()
  71.                 # broken socket, remove it
  72.                 if socket in SOCKET_LIST:
  73.                     SOCKET_LIST.remove(socket)
  74.  
  75. if __name__ == "__main__":
  76.  
  77.     sys.exit(chat_server())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement