logging outgoing HTTP POST message and incoming response message

S

scriptlearner

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
 
D

Diez B. Roggisch

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
 
S

scriptlearner

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.
 
G

Gabriel Genellina

En Wed, 22 Jul 2009 18:18:37 -0300, (e-mail address removed)
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, ...)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top