Created
April 10, 2015 15:21
-
-
Save leVirve/9b5c1f72cdef30c6faa3 to your computer and use it in GitHub Desktop.
Multithreading echo socket-server in Python3
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 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