-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathselect_echo_slow_client.py
50 lines (40 loc) · 1.09 KB
/
select_echo_slow_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
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann. All rights reserved.
#
"""Client half of echo example
"""
#end_pymotw_header
import socket
import sys
import time
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print('connecting to {} port {}'.format(*server_address),
file=sys.stderr)
sock.connect(server_address)
time.sleep(1)
messages = [
'Part one of the message.',
'Part two of the message.',
]
amount_expected = len(''.join(messages))
try:
# Send data
for message in messages:
data = message.encode()
print('sending {!r}'.format(data), file=sys.stderr)
sock.sendall(data)
time.sleep(1.5)
# Look for the response
amount_received = 0
while amount_received < amount_expected:
data = sock.recv(16)
amount_received += len(data)
print('received {!r}'.format(data), file=sys.stderr)
finally:
print('closing socket', file=sys.stderr)
sock.close()