多线程tcp服务器python

多线程tcp服务器python,python,multithreading,tcp,Python,Multithreading,Tcp,我正在尝试用python创建一个多线程TCP服务器。 接受新客户机后,它将转到新创建的线程。 然而,当早期的客户端发送数据时,看起来像是新创建的线程截获了数据,所以从服务器端看,似乎只有较新的客户端在说话 这是我的密码: Nbre = 1 class ClientThread(threading.Thread): def __init__(self, channel, connection): global Nbre Nbre = Nbre + 1

我正在尝试用python创建一个多线程TCP服务器。 接受新客户机后,它将转到新创建的线程。 然而,当早期的客户端发送数据时,看起来像是新创建的线程截获了数据,所以从服务器端看,似乎只有较新的客户端在说话

这是我的密码:

Nbre = 1

class ClientThread(threading.Thread):

   def __init__(self, channel, connection):
      global Nbre
      Nbre = Nbre + 1
      print("This is thread "+str(Nbre)+" speaking")
      self.channel = channel
      self.connection = connection
      threading.Thread.__init__(self)

   def run(self):
      print(connection[0]+':'+str(connection[1])+'<'+str(Nbre)'> Connected!')
      try:
         while True:
             data = self.channel.recv(1024)
             if data:
               print(connection[0]+':'+str(connection[1])+'<'+str(Nbre)+'> '+str(data.strip('\n')))
             else:
               break
      finally:
         print(connection[0]+':'+str(connection[1])+'<'+str(Nbre)+'> Exited!')
         self.channel.close()

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('', 4747))
server.listen(0)
while True:
   channel, connection = server.accept()
   ClientThread(channel, connection).start()
Nbre=1
类ClientThread(threading.Thread):
定义初始化(自、通道、连接):
全球丁腈橡胶
丁腈橡胶=丁腈橡胶+1
打印(“这是线程”+str(Nbre)+“正在说话”)
self.channel=channel
self.connection=连接
threading.Thread.\uuuuu init\uuuuuu(自)
def运行(自):
打印(连接[0]+':'+str(连接[1])+'Connected!')
尝试:
尽管如此:
data=self.channel.recv(1024)
如果数据:
打印(连接[0]+':'+str(连接[1])+'+str(data.strip('\n'))
其他:
打破
最后:
打印(连接[0]+':'+str(连接[1])+'Exited!')
self.channel.close()
服务器=socket.socket(socket.AF\u INET,socket.SOCK\u流)
server.setsockopt(socket.SOL_socket,socket.SO_REUSEADDR,1)
server.bind((“”,4747))
服务器。侦听(0)
尽管如此:
通道,连接=server.accept()
ClientThread(通道,连接).start()
以下是我在启动时得到的信息,telnet客户端为第一个客户端发送“Hello”,为第二个客户端发送“Bonjour”:

 $ python simple_thread.py 
This is thread 2 speaking  # new connection
127.0.0.1:33925> Connected!
127.0.0.1:33925<2> hello
This is thread 3 speaking  # new connection
127.0.0.1:33926> Connected!
127.0.0.1:33926<3> Bonjour # last connected says "Bonjour" (ok here)
127.0.0.1:33926<3> hello   # first connected re-send "hello" but in thread 3?!
$python simple\u thread.py
这是线程2#新连接
127.0.0.1:33925>已连接!
127.0.0.1:33925你好
这是线程3#新连接
127.0.0.1:33926>已连接!
127.0.0.1:33926“你好”#最后一次连接时说“你好”(此处ok)
127.0.0.1:33926你好#第一个连接重新发送“你好”,但在线程3中?!
为什么第二次发送的“hello”不是从线程2弹出的?以及如何使其发生,以便我可以从服务器端回复到适当的客户端


非常感谢!(这里的线程新手:/)

好消息是,它可能有效,但您的日志记录已被破坏。这里使用的是
Nbre
,它是线程数,而不是当前线程数:

           print(connection[0]+':'+str(connection[1])+'<'+str(Nbre)+'> '+str(data.strip('\n')))
连接
对象也存在类似问题。日志记录使用的是
connection
而不是
self.connection
。通常会出现错误,但由于底部的
while
循环创建了一个全局
连接
变量,因此取而代之的是该变量。因此,在日志记录中使用
self.connection

为了您自己的理智起见,我还建议您将日志提取到函数中,并使用
string.format

def log(self, message):
   print('{}:{}<{}> {}'.format(self.connection[0], str(self.connection[1]), self.number, message)
def日志(自身,消息):
打印({}:{}{})。格式(self.connection[0],str(self.connection[1]),self.number,message)

因此,您可以只编写
self.log('Thread start')
,而不是每次重复日志格式。

这正好解释了我遇到的问题,现在完全解决了。非常感谢!
def log(self, message):
   print('{}:{}<{}> {}'.format(self.connection[0], str(self.connection[1]), self.number, message)