pythonproxyurllib

Setting proxy to urllib.request (Python3)


How can I set proxy for the last urllib in Python 3. I am doing the next

from urllib import request as urlrequest
ask = urlrequest.Request(url)     # note that here Request has R not r as prev versions
open = urlrequest.urlopen(req)
open.read()

I tried adding proxy as follows :

ask=urlrequest.Request.set_proxy(ask,proxies,'http')

However I don't know how correct it is since I am getting the next error:

336     def set_proxy(self, host, type):
--> 337         if self.type == 'https' and not self._tunnel_host:
    338             self._tunnel_host = self.host
    339         else:

AttributeError: 'NoneType' object has no attribute 'type'

Solution

  • You should be calling set_proxy() on an instance of class Request, not on the class itself:

    from urllib import request as urlrequest
    
    proxy_host = 'localhost:1234'    # host and port of your proxy
    url = 'http://www.httpbin.org/ip'
    
    req = urlrequest.Request(url)
    req.set_proxy(proxy_host, 'http')
    
    response = urlrequest.urlopen(req)
    print(response.read().decode('utf8'))