I've been developing Python server using SocketServer.TCPServer and SocketServer.BaseRequestHandler base classes. I'm using host "localhost" and port 2304 (not used by any other program).
The problem is that my server wont respond to remote request. I'm using Amazon AWS and have an static IP address for access.
When I test the server on the Amazon local machine using eg. browser, I see that my server does it's stuff, but remote access stays blocked. No clue how to open 2304 port to be accessible from outside local machine.
Here is the whole code:
import threading, socket
import SocketServer, Queue
import globals
import transaction_pool
class CTSRSThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
if globals.terminating:
self.Respond("")
pCurrThread = threading.currentThread()
sData = self.request.recv(8192)
print "CTSRSThreadedTCPRequestHandler->RECV->OK!"
sResponse = "OK!"
self.Respond(sResponse)
print "CTSRSThreadedTCPRequestHandler->SEND->OK!"
#-------------------------------------------------------------------------
def Respond(self, sResponse):
self.request.send(sResponse)
#-------------------------------------------------------------------------
class CTSRSThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
allow_reuse_address = True
class CTSRSServer:
m_pServerThread = None
m_pServer = None
#----------------------------------------------------------
def StartServer(self):
try:
self.m_pServer = CTSRSThreadedTCPServer(("localhost", 2304), CTSRSThreadedTCPRequestHandler)
sIP, iPort = self.m_pServer.server_address
self.m_pServerThread = threading.Thread(target = self.m_pServer.serve_forever)
self.m_pServerThread.daemon = True
self.m_pServer.daemon_threads = True
self.m_pServerThread.start()
globals.system_log.info("[CTSRS]->StartServer()")
except Exception, e:
globals.system_log.info("[CTSRS]->StartServer() -> Exception: " + str(e))
return False
return True
#----------------------------------------------------------
def StopServer(self):
globals.system_log.info("[CTSRS]->StopServer()")
#----------------------------------------------------------
Of course it wont respond to remote requests, "localhost" means exactly what it says, it's the "local host". You have to bind to the external interface instead, or to the generic "catch-all" interface.
To bind to all interfaces use e.g.
self.m_pServer = CTSRSThreadedTCPServer(('', 2304), CTSRSThreadedTCPRequestHandler)
The empty string as the address makes the server listen on all interfaces.