Skip to content

Commit d8d6557

Browse files
committed
create a simple client we can use both for testing and for the presentation demos
1 parent d1befa7 commit d8d6557

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

resources/session02/simple_client.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import socket
2+
import sys
3+
4+
5+
def client(msg):
6+
server_address = ('localhost', 10000)
7+
sock = socket.socket(
8+
socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP
9+
)
10+
print >>sys.stderr, 'connecting to {0} port {1}'.format(*server_address)
11+
sock.connect(server_address)
12+
response = ''
13+
done = False
14+
bufsize = 1024
15+
try:
16+
print >>sys.stderr, 'sending "{0}"'.format(msg)
17+
sock.sendall(msg)
18+
while not done:
19+
chunk = sock.recv(bufsize)
20+
if len(chunk) < bufsize:
21+
done = True
22+
response += chunk
23+
print >>sys.stderr, 'received "{0}"'.format(response)
24+
finally:
25+
print >>sys.stderr, 'closing socket'
26+
sock.close()
27+
return response
28+
29+
30+
if __name__ == '__main__':
31+
if len(sys.argv) != 2:
32+
usg = '\nusage: python echo_client.py "this is my message"\n'
33+
print >>sys.stderr, usg
34+
sys.exit(1)
35+
36+
msg = sys.argv[1]
37+
client(msg)

0 commit comments

Comments
 (0)