Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # - - - - - - - - - - - - - - - - - - - - - -
- # Author: Spencer
- # Date: 9/24/2014
- # - - - - - - - - - - - - - - - - - - - - - -
- #Import modules for client
- import socket
- import threading
- #create socket
- client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- running = True
- #get info for connection
- name = input("Enter Username: ")
- if (name=="aaa"):
- name = "test"
- ip = "localhost"
- port = "55555"
- elif (name=="sss"):
- name = "TestClient"
- ip = "localhost"
- port = "55555"
- elif (name==""):
- name = "DefaultUser"
- print('defaulted to DefaultUser')
- else:
- ip = input("Enter IP: ")
- if (ip==""):
- ip = "localhost"
- print('defaulted to localhost')
- port = input("Enter Port: ")
- if (port==""):
- port = "55555"
- print('defaulted to 55555')
- #connect socket
- client_socket.connect((str(ip), int(port)))
- #Function to listen for messages from server, runs on its own thread
- def listen_for_message():
- while running:
- data = client_socket.recv(1024).decode('utf-8')
- print ("\r" + " " + "\r" + data)
- prompt()
- print("\nListener Exiting")
- #Make the listener object, and start it
- listener = threading.Thread(target=listen_for_message, args=(), daemon=False)
- listener.start()
- print("-------------------")
- #First thing sent will be the username
- data = name
- #Loop until an exit command is given
- while (data != ("/exit")):
- if (data != ""):
- client_socket.send(data.encode('utf-8'))
- data = ""
- data = input(name + ": ")
- #This switches off the listener
- running = False
- #Send Terminate message
- client_socket.send(("TERMINATE^&*").encode('utf-8'))
- #Look for the Terminate OK
- data = client_socket.recv(128).decode('utf-8')
- if (data == "DC OK*"):
- client_socket.close()
- print("Connection Closed")
Advertisement
Add Comment
Please, Sign In to add comment