logging outgoing HTTP POST message and incoming response message

Discussion in 'Python' started by scriptlearner@gmail.com, Jul 22, 2009.

  1. Guest

    I am sending a HTTP POST by using the following codes:

    opener = urllib2.build_opener(proxyHandler, MultipartPostHandler)
    params = { "audio" : "http://sample.com/my.wav",
    "data" : open(file, "rb") }
    print opener.open(myURL, params).read()

    How do I log or print out the actual POST message (including headers)
    that is sent out?
    How do I log or print out the response headers as well?

    Thanks
    , Jul 22, 2009
    #1
    1. Advertising

  2. schrieb:
    > I am sending a HTTP POST by using the following codes:
    >
    > opener = urllib2.build_opener(proxyHandler, MultipartPostHandler)
    > params = { "audio" : "http://sample.com/my.wav",
    > "data" : open(file, "rb") }
    > print opener.open(myURL, params).read()
    >
    > How do I log or print out the actual POST message (including headers)
    > that is sent out?
    > How do I log or print out the response headers as well?


    You can use proxy-tools such as tcpmon or sniff traffic using wireshark.

    Diez
    Diez B. Roggisch, Jul 22, 2009
    #2
    1. Advertising

  3. Guest

    On Jul 22, 1:54 pm, "Diez B. Roggisch" <> wrote:
    > You can use proxy-tools such as tcpmon or sniff traffic using wireshark.
    >
    > Diez


    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.
    , Jul 22, 2009
    #3
  4. En Wed, 22 Jul 2009 18:18:37 -0300,
    <> escribió:
    > On Jul 22, 1:54 pm, "Diez B. Roggisch" <> 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
    Gabriel Genellina, Jul 23, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Pedersen

    Outgoing GPRS while incoming call

    Simon Pedersen, Jun 27, 2005, in forum: Java
    Replies:
    0
    Views:
    429
    Simon Pedersen
    Jun 27, 2005
  2. rajatag
    Replies:
    10
    Views:
    581
    Esmond Pitt
    Feb 28, 2007
  3. Replies:
    0
    Views:
    190
  4. Jurjen de Groot

    WebService logging incoming/outgoing SOAP message

    Jurjen de Groot, Dec 24, 2007, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    412
    Stephan Brenner
    Dec 29, 2007
  5. Francis Hwang
    Replies:
    2
    Views:
    221
    Francis Hwang
    Apr 26, 2004
Loading...

Share This Page