Python: Urllib proxyhandler error

1.5k views Asked by At

So I'm fairly new to using the urllib.request library and I'm trying to run a proxy through proxy handler, however I keep getting this error message

assert hasattr(proxies, 'keys'), "proxies must be a mapping"
AssertionError: proxies must be a mapping

My code is

import urllib.request

proxy = "https://107.170.206.225"
handler = urllib.request.ProxyHandler(proxy)
opener = urllib.request.build_opener(handler)
urllib.request.install_opener(opener)
response = urllib.request.urlopen('http://youtube.com/')

I've tried looking through the documentation and it said to make sure to use the dictionary mapping protocol but I'm unsure of how to do that so any help would be appreciated.

1

There are 1 answers

0
lig On

You are right the documentation for ProxyHandler says that "proxies must be a dictionary mapping protocol names to URLs of proxies". And it must be read as "proxies must be a dictionary that maps protocol names to URLs of proxies". In your case proxies should be defined as follows.

proxies = {'https': '107.170.206.225'}
handler = urllib.request.ProxyHandler(proxies)