-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
75 lines (60 loc) · 2.01 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import socket
import threading
import sys
import argparse
#TODO: Implement a client that connects to your server to chat with other clients here
lock = threading.Lock()
# Use sys.stdout.flush() after print statemtents
def parseCLA():
parser = argparse.ArgumentParser()
parser.add_argument('-join', action='store_true', help='join the server')
parser.add_argument('-host', required=True, help='host name of the client')
parser.add_argument('-port', type=int, required=True, help='port number to join')
parser.add_argument('-username', required=True, help='username of the client')
parser.add_argument('-passcode', required=True, help='Passcode for the server') #if no type specified, it is a string
args = parser.parse_args()
return args.host, args.port, args.username, args.passcode
def paddingString(text):
return text.ljust(100, " ").encode()
def sendMessage(socket, text):
socket.sendall(paddingString(text))
def receivingMes(socket):
while True:
text = socket.recv(100).decode()
if len(text) >= 100:
break
return text.strip()
def printToClient(socket):
try:
while True:
while True:
text = socket.recv(100).decode()
if len(text) >= 100:
break
print (text.strip())
sys.stdout.flush()
except:
return
def client_program(host, port, username, passcode):
client_socket = socket.socket() # instantiate
client_socket.connect((host, port)) # connect to the server
login = username + " " + passcode #construct username and passcode
sendMessage(client_socket, login)
response = receivingMes(client_socket)
print (response)
sys.stdout.flush()
if (response == "Incorrect passcode"):
client_socket.close()
return
t = threading.Thread(target=printToClient, args=[client_socket])
t.start()
while True:
text = input() # take input
sendMessage(client_socket, text)
if (text == 'Exit'):
client_socket.close()
break
#client_socket.close() close the connection
if __name__ == '__main__':
host, port, username, passcode = parseCLA()
client_program(host, port, username, passcode)