computercam

Server

Sep 13th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. import socket
  2. import sys
  3.  
  4. HOST = ''   # Symbolic name meaning all available interfaces
  5. PORT = 25565             # Arbitrary non-privileged port
  6. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7. s.bind((HOST, PORT))
  8. s.listen(5)
  9. print("Welcome to ChatSrv use /exit to quit and (usr) to view the name of the other user.")
  10. print("Server Started.")
  11. usrnme = raw_input("Enter a username: ")
  12. conn, addr = s.accept()
  13. print addr, " Joined. Waiting for MetaData."
  14. ClientUsr = conn.recv(1024)
  15. print "Client Username:", ClientUsr
  16. conn.sendall(usrnme)
  17. while 1:
  18.    
  19.     #Send a message
  20.     a = raw_input("Message to Send: ")
  21.     if a == "/exit":
  22.         print("Prepearing to close down.")
  23.         conn.sendall("EXIT")
  24.         conn.close()
  25.         sys.exit("--END--")
  26.     """if a == "/changename":
  27.        usrnme = raw_input("New username: ")
  28.        conn.sendall("newName")
  29.        conn.sendall(usrnme)"""
  30.     conn.sendall(usrnme + ": " + a)
  31.  
  32.    
  33.     #Recieve a message.
  34.     data = conn.recv(1024)
  35.     if data == "EXIT":
  36.         print("The Server has closed the connection.")
  37.         conn.close()
  38.         sys.exit("The chat server has been closed by the remote server")
  39.        
  40.    
  41.     print(data)
  42.  
  43. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment