Can Twisted be used as an interface between web/http and UDP?

22 Views Asked by At

I have a network device that speaks UDP. I want to develop a web front end for it. Can I use twisted to implement an API that will then send a UDP packet, wait for a reply, and then display the response to the web client?

I believe I can start/run a UDP and Web application at the same time, but I'm not sure how to get them to talk to each other. Any suggestions on how to approach this problem?

1

There are 1 best solutions below

0
Glyph On

Yes. This sort of integration is exactly what Twisted is for. Just make a https://docs.twistedmatrix.com/en/stable/api/twisted.web.server.Site.html and then call https://docs.twistedmatrix.com/en/stable/api/twisted.internet.interfaces.IReactorUDP.html#listenUDP with your UDP listener, and everything should work more or less out of the box.

In terms of getting them to talk to each other — they're both python objects. Just instantiate them with a reference to whatever data they need; maybe even a reference to each other, if you must. This is why all the objects within Twisted take a Factory of some kind. Give your web server a Resource object that references your UDP server, and vice versa.