Created
December 20, 2016 15:28
-
-
Save jbouzekri/1ce75bcc975a89ec6dbc51f60866ac14 to your computer and use it in GitHub Desktop.
python tcp echo server
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
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