gpt4 book ai didi

Python TCPServer 地址重用

转载 作者:行者123 更新时间:2023-11-28 18:46:16 26 4
gpt4 key购买 nike

import SocketServer
import sys
from Queue import *
import threading

class CustomTCPServer(SocketServer.TCPServer):
def __init__(self, server_address, RequestHandlerClass, commandQueue=Queue):
self.queue = commandQueue
SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate=False)
SocketServer.TCPServer.allow_reuse_address = True
self.server_bind()
self.server_activate()

class SingleTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
commandQueue = self.server.queue
self.data = self.request.recv(1024).strip()
try:
commandQueue.put(self.data)
except Queue.Empty:
print 'Sorry.. Cannot accept any more.. Queue is full..'

def main():
HOST = ''
PORT = 50099
commandQueue = Queue()
server = CustomTCPServer((HOST, PORT), SingleTCPHandler, commandQueue)
threadObject = threading.Thread(target=server.serve_forever)
threadObject.setDaemon(True)
threadObject.start()
threadObject.join()

if __name__ == '__main__':
main()

但是每当我第二次运行代码时,我都会收到这样的消息socket.error: [Errno 98] 地址已被使用

我很困惑..

任何建议将不胜感激..

最佳答案

代替:

SocketServer.TCPServer.allow_reuse_address = True

它为您已经从中继承了自己的基类设置了值,(并创建了一个实例),

使用:

self.allow_reuse_address = True

关于Python TCPServer 地址重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19603739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com