开发者

wxPython threaded UDP server

I am tr开发者_如何学Cying 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


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜