Tutorial :Problem making a GET request and spoof User-Agent in urllib2


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.



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


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »