Skip to content

Instantly share code, notes, and snippets.

@leVirve
Created April 10, 2015 15:21
Show Gist options
  • Save leVirve/9b5c1f72cdef30c6faa3 to your computer and use it in GitHub Desktop.
Save leVirve/9b5c1f72cdef30c6faa3 to your computer and use it in GitHub Desktop.
Multithreading echo socket-server in Python3
import socketserver
from threading import Thread
class service(socketserver.BaseRequestHandler):
def handle(self):
while True:
self.data = self.request.recv(1024).strip()
if not self.data:
break
print("{} wrote:".format(self.client_address))
print(self.data)
self.request.sendall(self.data)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
if __name__ == "__main__":
HOST, PORT = "localhost", 5566
server = ThreadedTCPServer((HOST, PORT), service)
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment