tags:

views:

20

answers:

1

Is it possible to inspect the attributes of an Python urllib2.Request (url, data, headers etc) when using an urllib2.OpenerDirector:

cookie_jar = cookielib.CookieJar()    
opener = urllib2.OpenerDirector()
opener.add_handler(urllib2.ProxyHandler())
opener.add_handler(urllib2.UnknownHandler())
opener.add_handler(urllib2.HTTPHandler())
opener.add_handler(urllib2.HTTPRedirectHandler())
opener.add_handler(urllib2.HTTPDefaultErrorHandler())
opener.add_handler(urllib2.HTTPSHandler())
opener.add_handler(urllib2.HTTPErrorProcessor())
opener.add_handler(urllib2.HTTPCookieProcessor(cookie_jar))
request = urllib2.Request('http://example.com')
response = opener.open(request)

The request object has no attributes set before being opened. Is there a way to access them?

+2  A: 

I'm not sure which attributes you're looking for exactly, but hopefully this answers your question. All of those attributes are in the Request class. To inspect the ones you listed, you can use these:

url = request.get_full_url()
data = request.get_data()
headers = request.headers

There are also functions to modify data/headers/etc.

More can be found in the docs: http://docs.python.org/library/urllib2.html#request-objects

landyman