-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
executable file
·43 lines (39 loc) · 1.09 KB
/
client.py
File metadata and controls
executable file
·43 lines (39 loc) · 1.09 KB
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
#! /usr/bin/env python3
import socket
import sys
def clientSocket(host, serv):
res = socket.getaddrinfo(host, serv, type=socket.SOCK_STREAM);
for r in res:
try:
sock = socket.socket(family=r[0], type=r[1], proto=r[2]);
sock.connect(r[4]);
return sock;
except Exception:
continue;
return None;
def main():
if len(sys.argv) < 4:
print('usage: ./client.py host port message [...]', file=sys.stderr);
sys.exit(1);
host = sys.argv[1];
port = sys.argv[2];
sock = clientSocket(host, port);
if sock == None:
print('Cannot connect', file=sys.stderr);
sys.exit(1);
msg = '';
for i in range(len(sys.argv) - 3):
if len(msg) != 0:
msg += ' ';
msg += sys.argv[3+i];
sock.send(msg.encode('utf-8'));
sock.shutdown(socket.SHUT_WR)
msg = b'';
data = sock.recv(512);
while len(data) != 0:
msg += data;
data = sock.recv(512);
print('From server:', msg.decode('utf-8'));
sock.close();
if __name__ == '__main__':
main();