Urllib.request.urlretrieve with proxy?

somehow, I cannot upload files through a proxy server, and I don’t know what I did wrong. I just get a timeout. Any tips?

import urllib.request urllib.request.ProxyHandler({"http" : "myproxy:123"}) urllib.request.urlretrieve("http://myfile", "file.file") 
+3
source share
1 answer

You need to use your proxy object, and not just create it (you created the object, but did not assign it to a variable and therefore cannot use it). Try using this template:

 #create the object, assign it to a variable proxy = urllib.request.ProxyHandler({'http': '127.0.0.1'}) # construct a new opener using your proxy settings opener = urllib.request.build_opener(proxy) # install the openen on the module-level urllib.request.install_opener(opener) # make a request urllib.request.urlretrieve('http://www.google.com') 

Or, if you do not need to rely on std-lib, use the requests (this code is from the official documentation):

 import requests proxies = {"http": "http://10.10.1.10:3128", "https": "http://10.10.1.10:1080"} requests.get("http://example.org", proxies=proxies) 
+10
source

Source: https://habr.com/ru/post/1434676/


All Articles