Python 3 - urllib, HTTP Error 407: Proxy Authentication Required

后端 未结 2 1063
走了就别回头了
走了就别回头了 2020-12-05 03:43

I\'m trying to open a website (I am behind a corporate proxy) using urllib.request.urlopen() but I am getting the error:

urllib.error.HTTPError: HTTP Error 4         


        
相关标签:
2条回答
  • 2020-12-05 03:57

    You can set the proxy server authentication with your credentials(username and password) to connect to the website using requests. This worked for me. To get your proxy server name: use

    import urllib.request as req
    import os
    #get your proxy server url details using below command
    req.getproxies() 
    
    #user your credentials and url to authenticate
    
    os.environ['http_proxy'] = "http://username:pwd@url:80"
    os.environ['https_proxy'] = "http://username:pwd@url:80"
    
    #replace username, pwd and url with your credentials.
    
    conn = req.urlopen('https://Google.com')
    return_str = conn.read()
    
    0 讨论(0)
  • 2020-12-05 04:04
    import urllib.request as req
    
    proxy = req.ProxyHandler({'http': r'http://username:password@url:port'})
    auth = req.HTTPBasicAuthHandler()
    opener = req.build_opener(proxy, auth, req.HTTPHandler)
    req.install_opener(opener)
    conn = req.urlopen('http://google.com')
    return_str = conn.read()
    
    0 讨论(0)
提交回复
热议问题