urllib.request.urlretrieve with proxy?

前端 未结 1 920
旧时难觅i
旧时难觅i 2020-12-09 18:41

somehow I can\'t download files trough a proxyserver, and I don\'t know what i have done wrong. I just get a timeout. Any advice?

import urllib.request

urll         


        
相关标签:
1条回答
  • 2020-12-09 19:18

    You need to use your proxy-object, not just instanciate it (you created an object, but didn't assign it to a variable and therefore can't use it). Try using this pattern:

    #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 the std-lib, use 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)
    
    0 讨论(0)
提交回复
热议问题