SocketClientThread 参考:http://eli.thegreenplace.net/2011/05/18/code-sample-socket-client-thread-in-python/ http://eli.thegreenplace.net/2011/05/18/code-sample-socket-client-thread-in-python/
Eli 提供了这个通用示例,说明如何在线程内管理套接字客户端,并使用队列模块与主作用域进行通信。 (再次感谢伊莱!)
一个线程工作得很好,但我想做的是同时管理多个 SocketClientThread 对象。
连接后,当我尝试向第二个对象发送数据时,它告诉我 Socket Client Thread.socket 没有 sendall 属性,因此我认为套接字已被清空。
1和2的服务器都成功接收连接,但触发错误的是第二个服务器的发送命令。
如何将此类与多个对象重复使用?
这是输出:
$ python testmodule.py
('sct1: ', 1, None)
('sct1: ', 1, 'tuxy')
('sct2: ', 1, None)
Exception in thread Thread-2:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
File "testmodule.py", line 59, in run
self.handlers[cmd.type](cmd)
File "testmodule.py", line 115, in _handle_SEND
self.socket.sendall(header + cmd.data)
AttributeError: 'NoneType' object has no attribute 'sendall'
这是我尝试打开两者的代码:
if __name__ == "__main__":
sct1 = SocketClientThread()
sct1.start()
sct1.cmd_q.put(ClientCommand(ClientCommand.CONNECT, ('', 50007)))
reply = sct1.reply_q.get(True)
sct1.cmd_q.put(ClientCommand(ClientCommand.SEND, "hellothere from sct1"))
reply = sct1.reply_q.get(True)
print('sct1: ', reply.type, reply.data)
sct1.cmd_q.put(ClientCommand(ClientCommand.RECEIVE, "hellothere from sct1"))
reply = sct1.reply_q.get(True)
print('sct1: ', reply.type, reply.data)
sct2 = SocketClientThread()
sct2.start()
sct2.cmd_q.put(ClientCommand(ClientCommand.CONNECT, ('', 50008)))
reply = sct2.reply_q.get(True)
print('sct2 connect: ', reply.type, reply.data)
sct2.cmd_q.put(ClientCommand(ClientCommand.SEND, "hellothere from sct2"))
reply = sct2.reply_q.get(True)
print('sct2 send: ', reply.type, reply.data)
sct2.cmd_q.put(ClientCommand(ClientCommand.RECEIVE, "hellothere from sct2"))
reply = sct2.reply_q.get(True)
print('sct2: ', reply.type, reply.data)
#close connection 1
sct1.cmd_q.put(ClientCommand(ClientCommand.CLOSE))
reply = sct1.reply_q.get(True)
print('sct1 close: ', reply.type, reply.data)
#close connection 2
sct2.cmd_q.put(ClientCommand(ClientCommand.CLOSE))
reply = sct2.reply_q.get(True)
print('sct2 close: ', reply.type, reply.data)