python 异步连接

发布于 2024-10-01 20:36:49 字数 105 浏览 0 评论 0原文

有没有办法检查 asyncore 中是否建立了连接?

一旦调用 asyncore.loop() ,如何断开服务器之间的通信?

我可以简单地调用 close() 吗?

is there a way to check connection is established in asyncore?

once asyncore.loop() is called, how can I disconnect the communication between server?

can i just simply call close()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏雨凉 2024-10-08 20:36:49

一旦调用 asyncore.loop(),控制权就会移交给运行的事件循环。

asyncore.loop() 之后的任何代码只有在事件循环关闭后才会被调用。

事件循环将对各种事件做出反应并调用处理程序。 要关闭事件循环,您必须在有意义的事件处理程序之一中调用 stop。

例如:请看以下示例。

代码来自: http://www.mechanicalcat.net/richard/log/Python/A_simple_asyncore__echo_server__example< /a>

import asyncore, socket

class Client(asyncore.dispatcher_with_send):
    def __init__(self, host, port, message):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))
        self.out_buffer = message

    def handle_close(self):
        self.close()

    def handle_read(self):
        print 'Received', self.recv(1024)
        self.close()

c = Client('', 5007, 'Hello, world')
asyncore.loop()

self.close 在事件处理程序之一 -handle_read 内调用。在这种情况下,从服务器接收到数据后。它会自行断开连接。

参考文献:

Once asyncore.loop() is called, the control is handed over to event loop that runs.

Any code after the asyncore.loop() will be called only after the event loop has been shut down.

Event loop will react to various events and call handlers. To shutdown the event loop, you must call to stop in one of the event handler where it makes sense.

For ex: Take a look at the following example.

Code from : http://www.mechanicalcat.net/richard/log/Python/A_simple_asyncore__echo_server__example

import asyncore, socket

class Client(asyncore.dispatcher_with_send):
    def __init__(self, host, port, message):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))
        self.out_buffer = message

    def handle_close(self):
        self.close()

    def handle_read(self):
        print 'Received', self.recv(1024)
        self.close()

c = Client('', 5007, 'Hello, world')
asyncore.loop()

self.close is called inside one of the event handler - handle_read. In this case after the data has been received from the server. it disconnects itself.

References:

葬シ愛 2024-10-08 20:36:49

有没有办法检查 asyncore 中是否建立了连接?

与套接字一起使用,您可以重载asyncore的handle_connect()方法。当套接字连接时调度程序运行:

import asyncore

class MyClientConnection(asyncore.dispatcher):
    def handle_connect(self):
        '''Socket is connected'''
        print "Connection is established"

如果您更喜欢轮询,请读取 asyncore.dispatcher 中的变量 connected

myclient = MyClientConnection()
isConnected = myClient.connected

is there a way to check connection is established in asyncore?

Using with sockets, you can overload handle_connect() method of asyncore.dispatcher to run when socket is connected:

import asyncore

class MyClientConnection(asyncore.dispatcher):
    def handle_connect(self):
        '''Socket is connected'''
        print "Connection is established"

If you prefer polling, read the variable connected in your asyncore.dispatcher:

myclient = MyClientConnection()
isConnected = myClient.connected
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文