简体   繁体   English

如何在Python 2.7.3中处理多个套接字并在它们之间发送数据?

[英]How would I handle multiple sockets and send data between them in Python 2.7.3?

I am trying to create a server in Python 2.7.3 which sends data to all client connections whenever one client connection sends data to the server. 我正在尝试在Python 2.7.3中创建一个服务器,每当一个客户端连接将数据发送到服务器时,该服务器就会将数据发送到所有客户端连接。 For instance, if client c3 sent "Hello, world!" 例如,如果客户c3发送"Hello, world!" to my server, I would like to then have my server send "Hello, world!" 到我的服务器上,我想让我的服务器发送"Hello, world!" to client connections c1 and c2. 到客户端连接c1和c2。 By client connections, I mean the communications sockets returned by socket.accept() . 通过客户端连接,我的意思是socket.accept()返回的通信套接字。 Note that I have tried using the asyncore and twisted modules, but AFAIK they do not support this. 请注意,我已经尝试使用 asyncoretwisted模块,但是AFAIK他们不支持此功能。 Does anybody know any way to accomplish this? 有人知道实现此目的的任何方法吗?

EDIT : I have seen Twisted, but I would much rather use the socket module. 编辑 :我已经看过Twisted,但是我宁愿使用socket模块。 Is there a way (possibly multithreading, possibly using select) that I can do this using the socket module? 有没有一种方法可以使用套接字模块执行此操作(可能是多线程,可能使用select)?

You can absolutely do this using Twisted Python. 您绝对可以使用Twisted Python做到这一点。 You just accept the connections and set up your own handling logic (of course the library does not including built-in support for your particular communication pattern exactly, but you can't expect that). 您只需要接受连接并设置自己的处理逻辑即可(当然,该库不完全包含对您的特定通信模式的内置支持,但是您不能期望这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM