wxPython线程UDP服务器

发布于 2024-09-11 00:51:23 字数 363 浏览 2 评论 0原文

我正在尝试将 UDP 服务器与 wxPython GUI 组合在一起。

这是代码的链接:

UDP Server Pastie.org

我已将其链接起来,因为它很漂亮冗长。我已成功让 UDP 服务器在线程上运行,但我不知道如何在停止线程时关闭套接字。

目前,每次您单击“开始”时,它都会启动一个新线程,但我将删除它。当线程停止时是否可以关闭套接字的运行?

如果我以完全错误的方式这样做,我们将不胜感激。

干杯

埃夫

I am trying to put together a UDP server with a wxPython GUI.

Here is a link to the code:

UDP Server pastie.org

I have linked it as its pretty lengthy. I have successfully got the UDP server running on the thread but I can not figure out how to close the socket when the stopping the thread.

At the moment it will kick up a new thread each time you click start but I will be removing this. Is it possible to close the socket from running when the thread is stopped?

If I am doing this the complete wrong way any advice is appreciated.

Cheers

Eef

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

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

发布评论

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

评论(1

楠木可依 2024-09-18 00:51:23

使用Python Twisted。它与twisted.internet.wxreactor 集成了wxPython,使网络变得简单且无线程。

from twisted.internet import wxreactor
from twisted.internet.protocol import DatagramProtocol

wxreactor.install()

class MyProtocol(DatagramProtocol):
    def datagramReceived(self, data, (host, port)):
        print "received %r from %s:%d" % (data, host, port)
        self.transport.write(data, (host, port))

# <GUI code>
# to start listening do port = reactor.listenUDP(<port>, MyProtocol())
# to stop do self.transport.stopListening() in MyProtocol
# or port.stopListening() from outside

from twisted.internet import reactor
reactor.registerWxApp(app)
reactor.run()

Use Python Twisted. It has wxPython integration with twisted.internet.wxreactor and makes networking easy and threadless.

from twisted.internet import wxreactor
from twisted.internet.protocol import DatagramProtocol

wxreactor.install()

class MyProtocol(DatagramProtocol):
    def datagramReceived(self, data, (host, port)):
        print "received %r from %s:%d" % (data, host, port)
        self.transport.write(data, (host, port))

# <GUI code>
# to start listening do port = reactor.listenUDP(<port>, MyProtocol())
# to stop do self.transport.stopListening() in MyProtocol
# or port.stopListening() from outside

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