Skip to content

Instantly share code, notes, and snippets.

@jbouzekri

jbouzekri/server.py

Created Dec 20, 2016
Embed
What would you like to do?
python tcp echo server
import sys
import socket
import threading
import SocketServer
from socket import error as SocketError
import errno
class SingleTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
while True:
try:
data = self.request.recv(1024) # clip input at 1Kb
except SocketError as e:
if e.errno != errno.ECONNRESET:
raise # Not error we are looking for
break
if data == '':
self.request.close()
break
print data
if not data.startswith('PROXY'):
self.request.sendall('answer : {}'.format(data))
class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
# Ctrl-C will cleanly kill all spawned threads
daemon_threads = True
# much faster rebinding
allow_reuse_address = True
def __init__(self, server_address, RequestHandlerClass):
SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass)
if __name__ == "__main__":
# Port 0 means to select an arbitrary unused port
HOST, PORT = "0.0.0.0", 10001
server = SimpleServer((HOST, PORT), SingleTCPHandler)
# terminate with Ctrl-C
try:
server.serve_forever()
except KeyboardInterrupt:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment