datsexyanon

Client

Sep 25th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. # - - - - - - - - - - - - - - - - - - - - - -
  2. #   Author: Spencer
  3. #   Date:   9/24/2014
  4. # - - - - - - - - - - - - - - - - - - - - - -
  5.  
  6. #Import modules for client
  7. import socket
  8. import threading
  9. #create socket
  10. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. running = True
  12. #get info for connection
  13. name = input("Enter Username: ")
  14. if (name=="aaa"):
  15.     name = "test"
  16.     ip = "localhost"
  17.     port = "55555"
  18. elif (name=="sss"):
  19.     name = "TestClient"
  20.     ip = "localhost"
  21.     port = "55555"
  22. elif (name==""):
  23.     name = "DefaultUser"
  24.     print('defaulted to DefaultUser')
  25. else:
  26.     ip = input("Enter IP: ")
  27.     if (ip==""):
  28.         ip = "localhost"
  29.         print('defaulted to localhost')
  30.     port = input("Enter Port: ")
  31.     if (port==""):
  32.         port = "55555"
  33.         print('defaulted to 55555')
  34. #connect socket
  35. client_socket.connect((str(ip), int(port)))
  36. #Function to listen for messages from server, runs on its own thread
  37. def listen_for_message():
  38.     while running:
  39.         data = client_socket.recv(1024).decode('utf-8')
  40.         print ("\r" + "                                        " + "\r" + data)
  41.         prompt()
  42.     print("\nListener Exiting")
  43. #Make the listener object, and start it
  44. listener = threading.Thread(target=listen_for_message, args=(), daemon=False)
  45. listener.start()
  46. print("-------------------")
  47. #First thing sent will be the username
  48. data = name
  49. #Loop until an exit command is given
  50. while (data != ("/exit")):
  51.     if (data != ""):
  52.         client_socket.send(data.encode('utf-8'))
  53.         data = ""
  54.     data = input(name + ": ")
  55. #This switches off the listener
  56. running = False
  57. #Send Terminate message
  58. client_socket.send(("TERMINATE^&*").encode('utf-8'))
  59. #Look for the Terminate OK
  60. data = client_socket.recv(128).decode('utf-8')
  61. if (data == "DC OK*"):
  62.     client_socket.close()
  63.     print("Connection Closed")
Advertisement
Add Comment
Please, Sign In to add comment