-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatclient
42 lines (40 loc) · 1.26 KB
/
chatclient
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
#!/usr/bin/python3
import socket
import select
import sys
import re
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:
print("please use this Format: chatclient,HOST IP address:port NO, Nickname")
sys.exit(1)
args = str(sys.argv[1]).split(':')
host = str(args[0])
port = int(args[1])
nick = str(sys.argv[2])
s.connect((host, port))
MESSAGE= s.recv(1024).decode('utf-8')
print(MESSAGE)
s.sendall(('NICK '+nick).encode('utf-8'))
ok = s.recv(1024).decode('utf-8')
if re.search('Error',ok) or re.search('ERROR',ok):
print(ok)
sys.exit()
print(ok)
while True:
sockets = [sys.stdin, s]
read_sockets,write_sockets, error_sockets = select.select(sockets, [], [])
for sock in read_sockets:
if sock == s:
message = sock.recv(2048).decode('utf-8')
if re.search(r'Error',message) or re.search(r'ERROR',message):
print (message)
else:
message = message[4:]#stripping the message removing MSG and giving it to print out as per the protocol
print (message)
else:
message = sys.stdin.readline()
if message == '\n':
continue
else:
s.sendall(('MSG '+message).encode('utf-8'))
s.close()