SocketServer bind multiple server

python, python-2.7, socketserver

Solution

`server_forever` handles requests until an explicit shutdown() request.

Once `launchme.serve_forever()` called, the next ones will not be called. You need to call them in threads or in separated processes.

import SocketServer
from SocketServer import BaseRequestHandler
import threading

class HTTPSERVER(BaseRequestHandler):

    def handle(self):
        rec = self.request.recv(1024)
        if rec:
           self.request.send('Got something!')

launchme = SocketServer.TCPServer(('', 82),HTTPSERVER)
launchme2 = SocketServer.TCPServer(('', 81),HTTPSERVER)
launchme3 = SocketServer.TCPServer(('', 80),HTTPSERVER)
t1 = threading.Thread(target=launchme.serve_forever)
t2 = threading.Thread(target=launchme2.serve_forever)
t3 = threading.Thread(target=launchme3.serve_forever)
for t in t1, t2, t3: t.start()
for t in t1, t2, t3: t.join()

Problem

I'm trying to bind multiple servers with python's SocketServer module: ``` import SocketServer from SocketServer import BaseRequestHandler class HTTPSERVER(BaseRequestHandler): def handle(self): rec = self.request.recv(1024) if rec: self.request.send('Got something!') launchme = SocketServer.TCPServer(('', 82),HTTPSERVER) launchme2 = SocketServer.TCPServer(('', 81),HTTPSERVER) launchme3 = SocketServer.TCPServer(('', 80),HTTPSERVER) launchme.serve_forever() print 'reached first server' launchme2.serve_forever() print 'reached second server' launchme3.serve_forever() print 'reached third server' ``` When this script is launched nothing gets printed but all 3 ports are actually open: ``` root@user:/# netstat -pna|more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2751/python tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 2751/python tcp 0 0 0.0.0.0:82 0.0.0.0:* LISTEN 2751/python ``` But this script only serve 'Got something!' when the request is made on port 82 (first bind). What is the proper way with the SocketServer module to get all port to work?

Original source

Related problems