0

I'm working on a project that uses the Twisted web facilities, but not the high-level web framework. How can I get access to the HTTPChannel instance (the twisted protocol) that created a certain http.Request instance?

I see that the constructor of http.Request has the channel as an argument, but there is not method/property that further accesses it.

Also, while I can access the HTTPFactory instance from the channel via the factory property - can i access the factory directly from the request instance?

vonPetrushev
  • 5,457
  • 6
  • 39
  • 51

1 Answers1

1

It looks like the channel is available directly on the Request. Consider Request.__init__:

def __init__(self, channel, queued):
    """                                                                                                                                     
    @param channel: the channel we're connected to.                                                                                         
    @param queued: are we in the request queue, or can we start writing to                                                                  
        the transport?                                                                                                                      
    """
    self.notifications = []
    self.channel = channel
    self.queued = queued
    ...

self.channel = channel seems to be just what you're looking for.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
  • Yes, this is like the wrapper class i'm using. But maybe there's a reason why it's not accessible right away? – vonPetrushev Jun 06 '13 at 02:50
  • I don't understand this comment. That's `twisted.web.http.Request.__init__`, not a wrapper class. It has a `channel` attribute right on it. I don't know what "not accessible right away" means. – Jean-Paul Calderone Jun 06 '13 at 10:26
  • Well, with re-implementing a constructor you are creating a wrapper for the request to be used instead of the original implementation. "not accessible right away" - this means that with the original implementation of the request you can't access the channel with an instance. – vonPetrushev Jun 06 '13 at 10:49
  • That is *the* constructor. That is not a re-implementation of the constructor. I copy/pasted from the source code. – Jean-Paul Calderone Jun 06 '13 at 13:19
  • Yes. You are correct. My problem appeared when I try to access the `channel` from `request` _outside_ the `process` method. I'm accepting this answer and I'll post another question soon explaining what exactly is happening. Thank you for your patience. – vonPetrushev Jun 06 '13 at 17:55