我正在使用 ThreadedTCPServer 来启动 TCP 服务器。这是代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import threading
import SocketServer
import time
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
recv1 = self.request.recv(1)
print "server: %s" % recv1
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
if __name__ == "__main__":
server = ThreadedTCPServer(('0.0.0.0', 8080), ThreadedTCPRequestHandler)
print 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
time.sleep(30)
server.shutdown()
print "end"
我正在使用Windows7(IP:192.168.1.180)/Python2.7,启动程序时,我可以从本地远程登录服务器,但从另一台机器上,我无法远程登录成功。
> telnet 192.168.1.180 8080
但是,我在 Linux 上运行该程序,它运行良好。我可以从另一台机器上成功地远程登录它。
为什么在 Windows7 上无法接收远程连接?我在运行期间检查了windows7上的网络状态:
C:\Users\Henry>netstat -ant | findstr 8080
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING InHost
抱歉,问题已解决。这是一个环境问题。它被windows7防火墙阻止。