forked from xysun/pychat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pychat_client.py
45 lines (38 loc) · 1.43 KB
/
pychat_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
import select, socket, sys
from pychat_util import Room, Hall, Player
import pychat_util
READ_BUFFER = 4096
if len(sys.argv) < 2:
print("Usage: Python3 client.py [hostname]", file = sys.stderr)
sys.exit(1)
else:
server_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_connection.connect((sys.argv[1], pychat_util.PORT))
def prompt():
print('>', end=' ', flush = True)
print("Connected to server\n")
msg_prefix = ''
socket_list = [sys.stdin, server_connection]
while True:
read_sockets, write_sockets, error_sockets = select.select(socket_list, [], [])
for s in read_sockets:
if s is server_connection: # incoming message
msg = s.recv(READ_BUFFER)
if not msg:
print("Server down!")
sys.exit(2)
else:
if msg == pychat_util.QUIT_STRING.encode():
sys.stdout.write('Bye\n')
sys.exit(2)
else:
sys.stdout.write(msg.decode())
if 'Please tell us your name' in msg.decode():
msg_prefix = 'name: ' # identifier for name
else:
msg_prefix = ''
prompt()
else:
msg = msg_prefix + sys.stdin.readline()
server_connection.sendall(msg.encode())