forked from johnrudolph08/PyWeb-02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_client.py
More file actions
40 lines (35 loc) · 1.02 KB
/
simple_client.py
File metadata and controls
40 lines (35 loc) · 1.02 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
import socket
import sys
def client(msg):
server_address = ('localhost', 10001)
sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP
)
print(
'connecting to {0} port {1}'.format(*server_address),
file=sys.stderr
)
sock.connect(server_address)
response = ''
done = False
bufsize = 1024
try:
print('sending "{0}"'.format(msg), file=sys.stderr)
sock.sendall(msg.encode('utf8'))
while not done:
chunk = sock.recv(bufsize)
if len(chunk) < bufsize:
done = True
response += chunk.decode('utf8')
print('received "{0}"'.format(response), file=sys.stderr)
finally:
print('closing socket', file=sys.stderr)
sock.close()
return response
if __name__ == '__main__':
if len(sys.argv) != 2:
usg = '\nusage: python echo_client.py "this is my message"\n'
print(usg, file=sys.stderr)
sys.exit(1)
msg = sys.argv[1]
client(msg)