Skip to content

Commit 0d8e6ca

Browse files
committed
convert to pluggable log buffer
1 parent 608223b commit 0d8e6ca

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

resources/session01/echo_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
import sys
33

44

5-
def client(msg):
5+
def client(msg, log_buffer=sys.stderr):
66
server_address = ('localhost', 10000)
77
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8-
print >>sys.stderr, 'connecting to {0} port {1}'.format(*server_address)
8+
print >>log_buffer, 'connecting to {0} port {1}'.format(*server_address)
99
sock.connect(server_address)
1010
try:
1111
# Send data
12-
print >>sys.stderr, 'sending "{0}"'.format(msg)
12+
print >>log_buffer, 'sending "{0}"'.format(msg)
1313
sock.sendall(msg)
1414
# Look for the response
1515
amount_received = 0
1616
amount_expected = len(msg)
1717
while amount_received < amount_expected:
1818
data = sock.recv(16)
1919
amount_received += len(data)
20-
print >>sys.stderr, 'received "{0}"'.foramt(data)
20+
print >>log_buffer, 'received "{0}"'.foramt(data)
2121
finally:
22-
print >>sys.stderr, 'closing socket'
22+
print >>log_buffer, 'closing socket'
2323
sock.close()
2424

2525

resources/session01/echo_server.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22
import sys
33

44

5-
def server():
5+
def server(log_buffer=sys.stderr):
66
address = ('127.0.0.1', 10000)
77
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
88
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
9-
print >>sys.stderr, "making a server on {0}:{1}".format(*address)
9+
print >>log_buffer, "making a server on {0}:{1}".format(*address)
1010
sock.bind(address)
1111
sock.listen(1)
1212

1313
try:
1414
while True:
15-
print >>sys.stderr, 'waiting for a connection'
15+
print >>log_buffer, 'waiting for a connection'
1616
conn, addr = sock.accept() # blocking
1717
try:
18-
print >>sys.stderr, 'connection - {0}:{1}'.format(*addr)
18+
print >>log_buffer, 'connection - {0}:{1}'.format(*addr)
1919
while True:
2020
data = conn.recv(16)
21-
print >>sys.stderr, 'received "{0}"'.format(data)
21+
print >>log_buffer, 'received "{0}"'.format(data)
2222
if data:
2323
msg = 'sending data back to client'
24-
print >>sys.stderr, msg
24+
print >>log_buffer, msg
2525
conn.sendall(data)
2626
else:
2727
msg = 'no more data from {0}:{1}'.format(addr)
28-
print >>sys.stderr, msg
28+
print >>log_buffer, msg
2929
break
3030
finally:
3131
conn.close()

0 commit comments

Comments
 (0)