Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:SocketServer没有套接字_Python_Sockets_Python 3.x_Socketserver - Fatal编程技术网

Python:SocketServer没有套接字

Python:SocketServer没有套接字,python,sockets,python-3.x,socketserver,Python,Sockets,Python 3.x,Socketserver,我想使用Python socketserver来等待消息,但要定期超时并进行一些其他处理。据我所知,以下代码应该可以工作,但是调用handle_request()会引发AttributeError异常,抱怨MyTCPServer对象没有属性“socket”。我做错了什么 import socketserver class SingleTCPHandler(socketserver.BaseRequestHandler): # One instance per connection.

我想使用Python socketserver来等待消息,但要定期超时并进行一些其他处理。据我所知,以下代码应该可以工作,但是调用handle_request()会引发AttributeError异常,抱怨MyTCPServer对象没有属性“socket”。我做错了什么

import socketserver

class SingleTCPHandler(socketserver.BaseRequestHandler):
    # One instance per connection.  Override handle(self) to customize action.
    def handle(self):
        # self.request is the client connection
        data = self.request.recv(1024)  # clip input at 1Kb
        print ("Received data: " + data.decode())
        self.request.close()

class MyTCPServer(socketserver.BaseServer):

    def __init__(self, serverAddress, handler):
        super().__init__(serverAddress, handler)

    def handle_timeout(self):
        print ("No message received in {0} seconds".format(self.timeout))

if __name__ == "__main__":
    print ("SocketServerWithTimeout.py")
    tcpServer = MyTCPServer(("127.0.0.1", 5006), SingleTCPHandler)
    tcpServer.timeout = 5

    loopCount = 0
    while loopCount < 5:
        tcpServer.handle_request()
        print ("Back from handle_request")
        loopCount = loopCount + 1
导入socketserver
类SingleTCPHandler(socketserver.BaseRequestHandler):
#每个连接一个实例。重写句柄(自)以自定义操作。
def句柄(自身):
#self.request是客户端连接
data=self.request.recv(1024)#以1Kb的速度剪辑输入
打印(“接收到的数据:+data.decode())
self.request.close()
类MyTCPServer(socketserver.BaseServer):
def uuu init uuu(self、serverAddress、handler):
super()
def句柄_超时(自身):
打印(“在{0}秒内未收到任何消息”。格式(self.timeout))
如果名称=“\uuuuu main\uuuuuuuu”:
打印(“SocketServerWithTimeout.py”)
tcpServer=MyTCPServer(((“127.0.0.1”,5006),SingleTCPHandler)
tcpServer.timeout=5
循环计数=0
当循环计数小于5时:
tcpServer.handle_请求()
打印(“从句柄请求返回”)
loopCount=loopCount+1

socketserver.BaseServer
是UDP和TCP服务器的通用基类


如果您的服务器继承自
socketserver.TCPServer

Drat,则代码将正常工作。我知道这会让我看起来像个白痴。非常感谢!这不会让你看起来像个白痴。它让你看起来像一个正在学习的程序员。我已经做了将近50年了,我仍然在犯错误,所以不要觉得太糟糕!