Skip to content

Commit

Permalink
Complete Exercise 2-14: Graceful exit - implemented in simple tcp tim…
Browse files Browse the repository at this point in the history
…estamp server-client; and complete Exercise 2-15: built asynchronous tcp timestamp server using SocketServer, client adopted from SocketServer client example
  • Loading branch information
schedutron committed May 30, 2017
1 parent 48a5827 commit bae4db6
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Chap2/async_tsTclnt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python

from socket import *

HOST = raw_input("Enter host: ")
if not HOST:
HOST = "localhost"
PORT = raw_input("Enter port: ")
if not PORT:
PORT = 9000
else:
PORT = int(PORT)
BUFSIZ = 1024
ADDR = (HOST, PORT)

while True:
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
data = raw_input('-|-> ')
if not data:
break
tcpCliSock.send("%s\r\n" % data)
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print data.strip()
tcpCliSock.close()
27 changes: 27 additions & 0 deletions Chap2/async_tsTserv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!usr/bin/env python

from time import ctime
import SocketServer

class AsyncRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
print "Connection from: %s" % str(self.client_address)
request_msg = self.rfile.readline(1024)
self.wfile.write("[%s] %s" % (ctime(), request_msg))
self.wfile.flush()

def up_and_run():
tcp_server = SocketServer.ForkingTCPServer(
("0.0.0.0", 9000),
RequestHandlerClass=AsyncRequestHandler,
bind_and_activate=False)

tcp_server.allow_reuse_address = True
tcp_server.server_bind()
tcp_server.server_activate()

tcp_server.serve_forever()

if __name__ == "__main__":
print "Waiting for connection..."
up_and_run()
34 changes: 34 additions & 0 deletions Chap2/graceful_exit_clnt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python

from socket import *

HOST = raw_input('Enter host: ')
if not HOST:
HOST = 'localhost'
PORT = raw_input('Enter port: ')
if not PORT:
PORT = 21567
else:
PORT = int(PORT)

BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

try:
while True:
data = raw_input('-|-> ')
if not data:
break
tcpCliSock.send(data)
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print data
except KeyboardInterrupt:
pass
finally:
print "\nShutting down client..."
tcpCliSock.close()
32 changes: 32 additions & 0 deletions Chap2/graceful_exit_serv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python

from socket import *
from time import ctime

HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

try:
while True:
print "Waiting for connection..."
tcpCliSock, addr = tcpSerSock.accept()
print "... connection from:", addr

while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
tcpCliSock.send('[%s] %s' % (ctime(), data))
tcpCliSock.close()
tcpSerSock.close()
except KeyboardInterrupt:
pass
finally:
print "\nShutting down server..."
tcpSerSock.close()

0 comments on commit bae4db6

Please sign in to comment.