我有一个 server.py 套接字代码和 client.py。现在我需要客户端始终准备好(处于待机状态)以接收数据,即使它没有向服务器发送任何数据。可以这样做吗?
问问题
163 次
2 回答
0
Well, I suppose what you need is select module.
http://www.doughellmann.com/PyMOTW/select/
The idea is that with the help of select you can wait for different types of events (read, write, exception) that happen on sockets passed to select's input.
于 2012-11-26T20:24:55.450 回答
0
我用它来接收从一些 C# 代码发送的数据,这些代码是我在 Python 中处理的。当您使用接收到的数据在其他线程中执行操作时,它使用线程“永远监听”。希望这可以帮助:
import threading
import SocketServer
class UDPHandler(SocketServer.BaseRequestHandler):
def handle(self):
## DO SOMETHING USEFUL WITH WHAT YOU RECEIVE VIA self:
data = self.request[0].strip() # in this case, self.request[0] contains a string
# etc.
class Listener(threading.Thread):
def run(self):
HOST = "192.168.1.23"
PORT = 8888
server = SocketServer.UDPServer((HOST, PORT), UDPHandler)
server.serve_forever()
Listener().start()
于 2012-11-26T20:31:35.160 回答