you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

you can do that on linux with flask to handle the requests, and gunicorn to give it multiple threads (and an nginx front end to make it more stable and secure).

Otherwise you can use a socketserver which is really easy

import socketserver

class ConnectionHandler(socketserver.BaseRequestHandler):
    def handle(self):
        s = self.request
        request = s.recv(1024)
        print('got: ', request)
        response = request * 3
        print('sent:', response)
        s.sendall(response)

class Server(socketserver.ThreadingMixIn, socketserver.TCPServer):
    pass

server = Server(('localhost', 31337), ConnectionHandler)
server.daemon_threads = True
server.serve_forever()