From your Python code, you would like to pretend to the web server that you are browsing from Mozilla Firefox.
You can send the custom user-agent values in the HTTP request header.
Listing 4.7 explains spoofing Mozilla Firefox in your client code as follows:
#!/usr/bin/env python # Python Network Programming Cookbook -- Chapter – 4 # This program is optimized for Python 2.7. # It may run on any other version with/without modifications. import urllib2 BROWSER = 'Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0' URL = 'http://www.python.org' def spoof_firefox(): opener = urllib2.build_opener() opener.addheaders = [('User-agent', BROWSER)] result = opener.open(URL) print "Response headers:" for header in result.headers.headers: print " ",header if __name__ == '__main__': spoof_firefox()
If you run this script, you will see the following output:
$ python 4_7_spoof_mozilla_firefox_in_client_code.py Response headers: Date: Sun, 05 May 2013 16:56:36 GMT Server: Apache/2.2.16 (Debian) Last-Modified: Sun, 05 May 2013 00:51:40 GMT ETag: "105800d-5280-4dbedfcb07f00" Accept-Ranges: bytes Content-Length: 21120 Vary: Accept-Encoding Connection: close Content-Type: text/html
3.133.146.47