我一直在使用 SocketServer.TCPServer 和 SocketServer.BaseRequestHandler 基类开发 Python 服务器。我正在使用主机“localhost”和端口 2304(未被任何其他程序使用)。
问题是我的服务器不会响应远程请求。我正在使用 Amazon AWS 并有一个静态 IP 地址用于访问。
当我使用例如在亚马逊本地机器上测试服务器时。浏览器,我看到我的服务器做了它的东西,但远程访问仍然被阻止。不知道如何打开 2304 端口以从本地机器外部访问。
这是整个代码:
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()")
#----------------------------------------------------------