tags:

views:

35

answers:

1

Hello,

With this code, urllib2 make a GET request:

#!/usr/bin/python
import urllib2
req = urllib2.Request('http://www.google.fr')
req.add_header('User-Agent', '')
response = urllib2.urlopen(req)

With this one (which is almost the same), a POST request:

#!/usr/bin/python
import urllib2
headers = { 'User-Agent' : '' }
req = urllib2.Request('http://www.google.fr', '', headers)
response = urllib2.urlopen(req)

My question is: how can i make a GET request with the second code style ?

The documentation (http://docs.python.org/release/2.6.5/library/urllib2.html) says that

headers should be a dictionary, and will be treated as if add_header() was called with each key and value as arguments

Yeah, except that in order to use the headers parameter, you have to pass data, and when data is passed, the request become a POST.

Any help will be very appreciated.

+2  A: 

Use:

req = urllib2.Request('http://www.google.fr', None, headers)

or:

req = urllib2.Request('http://www.google.fr', headers=headers)
Alex Martelli
perfect, thank you