-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathselect_echo_multiclient.py
51 lines (44 loc) · 1.26 KB
/
select_echo_multiclient.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
51
#!/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
messages = [
'This is the message. ',
'It will be sent ',
'in parts.',
]
server_address = ('localhost', 10000)
# Create a TCP/IP socket
socks = [
socket.socket(socket.AF_INET, socket.SOCK_STREAM),
socket.socket(socket.AF_INET, socket.SOCK_STREAM),
]
# Connect the socket to the port where the server is listening
print('connecting to {} port {}'.format(*server_address),
file=sys.stderr)
for s in socks:
s.connect(server_address)
for message in messages:
outgoing_data = message.encode()
# Send messages on both sockets
for s in socks:
print('{}: sending {!r}'.format(s.getsockname(),
outgoing_data),
file=sys.stderr)
s.send(outgoing_data)
# Read responses on both sockets
for s in socks:
data = s.recv(1024)
print('{}: received {!r}'.format(s.getsockname(),
data),
file=sys.stderr)
if not data:
print('closing socket', s.getsockname(),
file=sys.stderr)
s.close()