Skip to content

Instantly share code, notes, and snippets.

@AvocadoStyle
Created August 12, 2023 23:34
Show Gist options
  • Save AvocadoStyle/73b166293b9ca371cdea7307369cbbff to your computer and use it in GitHub Desktop.
Save AvocadoStyle/73b166293b9ca371cdea7307369cbbff to your computer and use it in GitHub Desktop.
Simple echo server
import socket
import threading
import socketserver
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
data = str(self.request.recv(1024), 'ascii')
cur_thread = threading.current_thread()
print(f"got data: {data} from client {cur_thread}")
response = bytes("{}: {}".format(cur_thread.name, data), 'ascii')
self.request.sendall(response)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
def client(ip, port, message):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((ip, port))
sock.sendall(bytes(message, 'ascii'))
response = str(sock.recv(1024), 'ascii')
print("Received: {}".format(response))
if __name__ == "__main__":
# Port 0 means to select an arbitrary unused port
HOST, PORT = "localhost", 8085
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
with server:
ip, port = server.server_address
# Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.daemon = True
server_thread.start()
print("Server loop running in thread:", server_thread.name)
server_thread.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment