logging outgoing HTTP POST message and incoming response message

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Jul 23 00:27:15 EDT 2009


En Wed, 22 Jul 2009 18:18:37 -0300, scriptlearner at gmail.com
<scriptlearner at gmail.com> escribió:
> On Jul 22, 1:54 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> You can use proxy-tools such as tcpmon or sniff traffic using wireshark.
>
> Thanks,
> but I am trying to enable some debug mode to log all outgoing and
> incoming messages for certain period of time, and running another
> proxy-tool is not very ideal.  It would be great to log it in some log
> file.

You may install a custom HTTPHandler:

class LoggingHTTPConnection(httplib.HTTPConnection):

      def request(self, method, url, body=None, headers={}):
          print method, url, headers
          httplib.HTTPConnection.request(self, method, url, body, headers)

      def getresponse(self):
          response = httplib.HTTPConnection.getresponse(self)
          print response.status, response.msg
          return response

class LoggingHTTPHandler(urllib2.HTTPHandler):
      def http_open(self, req):
          return self.do_open(LoggingHTTPConnection, req)

opener = urllib2.build_opener(LoggingHTTPHandler, ...)

-- 
Gabriel Genellina




More information about the Python-list mailing list