[Twisted-web] manually inject data into a site object
glyph at twistedmatrix.com
Thu Dec 29 18:26:46 EST 2011
On Dec 26, 2011, at 5:48 PM, gary jefferson wrote:
> I have a web service written in twisted-web.
> I have it running behind a NAT.
> I have a component (based on twisted Protocol) that makes a connection
> out from behind the NAT to a relay. Let's call this ReverseThing.
Since you're mentioning Twisted and NAT, I feel obliged to direct your attention to Vertex, which sorta solves some of these problems but badly needs a maintainer:
It may not be relevant to your use-case though (and isn't relevant to your question at all).
> Web clients can connect to the relay to send data back over this ReverseThing.
> Once I receive the data, how can I inject it directly into the web
> service? Assume both these services run in the same twisted process.
> Here's what I've tried: I can pass in the Site object to the
> ReverseThing protocol, so that I can call p=buildProtocol() on it,
> then I can call dataReceived() manually on that p. But obviously this
> doesn't work, because there isn't really a transport object to call
> things like getPeer() on.
You must call makeConnection(transport) on a Protocol before you call dataReceived. So make a Transport object - you don't need a "real" one that represents a socket, just something that implements <twistedmatrix.com/documents/current/api/twisted.internet.interfaces.ITransport.html>.
Many tests use in-memory or otherwise faked transport objects. Protocols must be able to deal with alternate transport implementations, at the very least in order to be able to use encrypted connections. As of Twisted 11.1, the TLS transport is always a different object, presuming you have a recent enough version of pyOpenSSL.
> Is there some other way to send data to a Site, without it actually
> coming over the wire?
You're 90% of the way there already, and you have the right idea :).
More information about the Twisted-web