停止 UDP 套接字服务器
我正在创建一个 UDP SocketServer,如果我从客户端收到关闭消息,我想关闭,而不是关闭 UDP 连接。
当我收到消息时,将显示 Begin
,但不显示 Finished
。我怎样才能退出这个线程?
class MyUDPHandler( SocketServer.BaseRequestHandler ):
def handle( self ):
data = self.request[0].strip()
dic = self.string_XML_to_dic( data )
if( dic['Cmd'] == str(UDPConst().SHUT_DOWN )):
print('Begin')
self.server.shutdown()
print("Finished")
if __name__ == "__main__":
HOST, PORT = "", prop['udpport']
server = SocketServer.UDPServer( ( HOST, PORT ), MyUDPHandler )
server.serve_forever()
I'm creating an UDP SocketServer, and I would like to shutdown if I'm receiving a close message from the client side than I would like to close the UDP connection.
As I received the message the Begin
will be displayed, but the Finished
is not displayed. How can I exit from this thread?
class MyUDPHandler( SocketServer.BaseRequestHandler ):
def handle( self ):
data = self.request[0].strip()
dic = self.string_XML_to_dic( data )
if( dic['Cmd'] == str(UDPConst().SHUT_DOWN )):
print('Begin')
self.server.shutdown()
print("Finished")
if __name__ == "__main__":
HOST, PORT = "", prop['udpport']
server = SocketServer.UDPServer( ( HOST, PORT ), MyUDPHandler )
server.serve_forever()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您没有在自己的线程中运行服务器(为此您需要继承 ThreadingMixIn ),因此您不能使用 shutdown ,因为它会导致死锁。从源中的函数文档注释:
As you are not running the server in it's own thread (you need to inherit
ThreadingMixIn
for that), you can not useshutdown
as it will cause a deadlock. From the function document comment in the source:说明:
= stop_listener 被调用。
Explanation:
= stop_listener gets called.