forked from schedutron/CPAP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete Exercise 2-14: Graceful exit - implemented in simple tcp tim…
…estamp server-client; and complete Exercise 2-15: built asynchronous tcp timestamp server using SocketServer, client adopted from SocketServer client example
- Loading branch information
1 parent
48a5827
commit bae4db6
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |