Getting headers with a proxy

We can use a proxy connection for the same task. If we need to specify a proxy in the code, we have to use an opener that contains the ProxyHandler handler. The default handler includes a ProxyHandler instance built by calling the initializer without parameters, which reads the list of proxies to use from the appropriate environment variable. However, we can also build a ProxyHandler, passing as a parameter a dictionary whose key is the HTTP protocol and the value is the proxy address or URL used for this protocol.

To install the opener once created, the install_opener function is used, which takes as a parameter the opener to be installed.

You can find the following code in the proxy_web_request.py file:

import urllib.request, urllib.parse, urllib.error
URL = 'https://www.github.com'
# By Googling free proxy server
PROXY_ADDRESS = "165.24.10.8:8080"
if __name__ == '__main__':
proxy = urllib.request.ProxyHandler({"http" : PROXY_ADDRESS})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
resp = urllib.request.urlopen(URL)
print ("Proxy server returns response headers: %s " %resp.headers)
..................Content has been hidden....................

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