How to do it...

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 requires Python 3.5.2 or any later version 
# It may run on any other version with/without modifications. 
# 
# Follow the comments inline to make it run on Python 2.7.x. 
 
import urllib.request, urllib.error, urllib.parse 
# Comment out the above line and uncomment the below for Python 2.7.x. 
#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 = urllib.request.build_opener() 
    # Comment out the above line and uncomment the below for Python 2.7.x. 
    #opener = urllib2.build_opener() 
 
    opener.addheaders = [('User-agent', BROWSER)] 
    result = opener.open(URL) 
    print ("Response headers:") 
 
    for header in  result.headers: 
    # Comment out the above line and uncomment the below for Python 2.7.x. 
    #for header in  result.headers.headers: 
        print ("%s: %s" %(header, result.headers.get(header))) 
        # Comment out the above line and uncomment the below for
Python 2.7.x. #print (header) if __name__ == '__main__': spoof_firefox()

If you execute this script, you will see the following output:

$ python 4_7_spoof_mozilla_firefox_in_client_code.py 
Response headers:
Server: nginx
    
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
X-Clacks-Overhead: GNU Terry Pratchett
Content-Length: 47755
Accept-Ranges: bytes
Date: Thu, 22 Jun 2017 15:38:39 GMT
Via: 1.1 varnish
Age: 834
Connection: close
X-Served-By: cache-ams4150-AMS
X-Cache: HIT
X-Cache-Hits: 1
X-Timer: S1498145920.740508,VS0,VE2
Vary: Cookie
Strict-Transport-Security: max-age=63072000; includeSubDomains
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.103.210