Skip to content

Commit

Permalink
Create page279socket_server.py
Browse files Browse the repository at this point in the history
  • Loading branch information
georgiosdoumas authored Dec 28, 2019
1 parent 39393c7 commit ed7964e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions chap14/page279socket_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/python3.6
# Simple implementation based on the book Listing 14.3

from socketserver import TCPServer, StreamRequestHandler

class ClientHandler(StreamRequestHandler):
def handle(self):
addr = self.request.getpeername()
msg_to_client = 'Thank you for connecting to basic SocketServer'
print('Got connection from', addr)
self.wfile.write(bytes(msg_to_client, 'utf-8'))

listening_port = 1234
try:
socket_server = TCPServer(('', listening_port), ClientHandler)
print("Simple socketServer started, listening on port ",listening_port)
print("...[stop by Ctrl+C]")
socket_server.serve_forever()
except Exception as setuper:
print(" Could not start server, due to :", setuper)
except KeyboardInterrupt:
print("Server was stopped by a Ctrl+C. Exiting program")

0 comments on commit ed7964e

Please sign in to comment.