Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple multi-threaded python server that offers up a file with a delay per line
import socket
import time
import threading
import SocketServer
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = open("file", "r").readlines()
for line in data:
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
if __name__ == "__main__":
# Port 0 means to select an arbitrary unused port
HOST, PORT = "localhost", 8083
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
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
print "Server loop running in thread:",
while True:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.