-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
70 lines (58 loc) · 1.73 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
import socket
import threading
localhost = '127.0.0.1'
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# host = localhost
# port = 34443
# client.connect((host,port))
while True:
try:
host = input("Enter host address: ")
port = int(input("Enter port: "))
if host == 'local':
host = localhost
client.connect((host,port))
print('\nConnection success')
break
except Exception as e:
print('\nConnection error - check host and port (port must be 0-65535) ')
print(e)
def receive():
while True:
try:
message = client.recv(1024).decode('ascii')
if message == 'CLOSE_CONN':
close_conn()
elif message == 'NICK':
client.send(nickname.encode('ascii'))
else:
print(message)
except KeyboardInterrupt:
close_conn()
except:
# Close Connection When Error
print("An error occured!")
client.close()
break
def close_conn():
print('\nClosing connection')
client.close()
receive_thread.join()
write_thread.join()
def write():
while True:
try :
message = input('> ')
if message == "close connection":
close_conn()
message = '{}: {}'.format(nickname, message)
client.send(message.encode('ascii'))
except KeyboardInterrupt:
print('\nClosing connection')
client.close()
receive_thread.join()
write_thread.join()
receive_thread = threading.Thread(target=receive)
receive_thread.start()
write_thread = threading.Thread(target=write)
write_thread.start()