Advertisement
Vagrum

Server Chat Test

Feb 3rd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. import socket, thread
  2. def runcon(client, addr):
  3.     global CONNECTION_LIST
  4.     global SOCKET_LIST
  5.     while True:
  6.         try:
  7.             message = client.recv(1024)
  8.             if message == "/list":
  9.                 for listip in CONNECTION_LIST:
  10.                         client.send(listip)
  11.             else:
  12.                 for clientsocket in SOCKET_LIST:
  13.                     if clientsocket != client:
  14.                         clientsocket.send(message)
  15.         except:
  16.             CONNECTION_LIST.remove(addr[0])
  17.             SOCKET_LIST.remove(client)
  18.             print addr[0], "Closed!"
  19.             break
  20. if __name__ == '__main__':
  21.     CONNECTION_LIST = []
  22.     SOCKET_LIST = []
  23.     mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  24.     mysocket.bind(("0.0.0.0", 8287))
  25.     mysocket.listen(10)
  26.     while True:
  27.         try:
  28.             client,addr = mysocket.accept()
  29.         except KeyboardInterrupt:
  30.             mysocket.close()
  31.             exit("Socket server closed.")
  32.         print addr[0], "connected!"
  33.         CONNECTION_LIST.append(addr[0])
  34.         SOCKET_LIST.append(client)
  35.         try:
  36.             thread.start_new_thread(runcon,(client,addr,))
  37.         except:
  38.             "Error =>", addr[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement