gpt4 book ai didi

python SocketServer.BaseRequestHandler 知道端口并使用已经打开的端口

转载 作者:行者123 更新时间:2023-11-28 19:38:09 28 4
gpt4 key购买 nike

这是我玩过的代码,但每次我犯错我都无法重新启动它。它对我说端口/套接字已被使用这是第一个问题第二个在我的 MyTCPHandler 中,我怎么知道使用的端口?这是我的代码:

# MetaProject v 0.2
# -*- coding: utf-8 -*-
"""
Thanks to :
People from irc :
Flox,Luyt
People from stack Overflow :
Philippe Leybaert,Platinum Azure,methodin,Suresh Kumar,S.Lott,MatTheCat,
kevpie,Ignacio Vazquez-Abrams,adamk,Frédéric Hamidi,THC4k,THC4k,Blam
"""
import SocketServer
import threading

class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(1024)
socket = self.request
print "%s wrote:" % self.client_address[0]
print self.data
print self.__dict__
socket.sendto(self.data.upper(), self.client_address)

def serve_thread(host, port):
server = SocketServer.TCPServer((host, port), MyTCPHandler)
server.serve_forever()

if __name__ == "__main__":
threading.Thread(target=serve_thread,args=('localhost', 1238)).start()
threading.Thread(target=serve_thread,args=('localhost', 1237)).start()
print "toto"

我做了:

def serve_thread(host, port):
if port == 1858 :
server = SocketServer.TCPServer((host, port), Serverhttp,bind_and_activate=True)
elif port == 1958 :
server = SocketServer.TCPServer((host, port), Serversmtp,bind_and_activate=True)
server.allow_reuse_address=True
server.serve_forever()

但它不起作用。问候

最佳答案

实际上比这更容易 -- 您可以将它设置为类变量,而不是重写 init。例如,

class MyServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
allow_reuse_address = True

关于python SocketServer.BaseRequestHandler 知道端口并使用已经打开的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3911009/

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