Corporate Proxy在c#中工作,但在Python中不行

本文关键字:但在 Python 工作 Proxy Corporate | 更新日期: 2023-09-27 18:13:28

我是一个公司代理,我正在尝试使用Python下载页面源。我的一个同事试着用c#写了一个类似的程序,它可以工作,但我的Python代码不起作用,尽管我们提供了相同的凭证。下面是c#代码:

class Program 
    { 
        static void Main(string[] args) 
        { 
            var netCred = new NetworkCredential { UserName = "asdf", Password = "pass", Domain = "Africa" }; 
            var webProxy = new WebProxy("corp_proxy:8080", true);   
            webProxy.Credentials = netCred; 
            using (WebClient client = new WebClient() { Proxy = webProxy }) 
            using (Stream data = client.OpenRead(@"http://www.google.com <http://www.google.com/> ")) 
            using (StreamReader reader = new StreamReader(data)) 
            { 
                client.Proxy = webProxy; 
                string s = reader.ReadToEnd(); 
                Console.WriteLine(s); 
            } 
            Console.ReadLine(); 
        } 
    }
下面是Python代码,
import urllib2
proxy_user = "Africa''asdf"
proxy_password = "pass"
proxy_port = "8080"
proxy_url = "corp_proxy"
def proxy_test():
  proxy_tot = 'http://' + proxy_user + ':' + proxy_password + '@' + proxy_url + ':' + proxy_port
  proxy = urllib2.ProxyHandler({"http":proxy_tot})
  auth = urllib2.HTTPBasicAuthHandler()
  opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
  urllib2.install_opener(opener)
  x = urllib2.urlopen('http://www.google.com')
  print x.read()
if __name__ == "__main__":
  proxy_test()

错误输出为

    Traceback (most recent call last):
  File ".'test.py", line 21, in <module>
    proxy_test()
  File ".'test.py", line 17, in proxy_test
    x = urllib2.urlopen('http://www.google.com')
  File "C:'Python27'Lib'urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "C:'Python27'Lib'urllib2.py", line 410, in open
    response = meth(req, response)
  File "C:'Python27'Lib'urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:'Python27'Lib'urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "C:'Python27'Lib'urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:'Python27'Lib'urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required

然后我尝试使用https,我收到的错误是:

Traceback (most recent call last):
  File ".'test.py", line 21, in <module>
    proxy_test()
  File ".'test.py", line 17, in proxy_test
    x = urllib2.urlopen('http://www.google.com')
  File "C:'Python27'Lib'urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "C:'Python27'Lib'urllib2.py", line 404, in open
    response = self._open(req, data)
  File "C:'Python27'Lib'urllib2.py", line 422, in _open
    '_open', req)
  File "C:'Python27'Lib'urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:'Python27'Lib'urllib2.py", line 722, in <lambda>
    meth(r, proxy, type))
  File "C:'Python27'Lib'urllib2.py", line 751, in proxy_open
    return self.parent.open(req, timeout=req.timeout)
  File "C:'Python27'Lib'urllib2.py", line 404, in open
    response = self._open(req, data)
  File "C:'Python27'Lib'urllib2.py", line 422, in _open
    '_open', req)
  File "C:'Python27'Lib'urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:'Python27'Lib'urllib2.py", line 1222, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "C:'Python27'Lib'urllib2.py", line 1184, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23
_GET_SERVER_HELLO:unknown protocol>

Python代码中有什么问题?

Corporate Proxy在c#中工作,但在Python中不行

由于您的代理使用NTLM身份验证,因此您必须使用兼容的AuthHandler,如ProxyNtlmAuthHandler

如果您不是绝对需要使用urllib2,请求可能会使它更容易。

import requests
proxy_user = "Africa''asdf"
proxy_password = "pass"
proxy_url = "http://corp_proxy:8080"
def proxy_test():
    proxy = {'http': proxy_url}
    auth = HTTPProxyAuth(proxy_user, proxy_password)
    r = requests.get('http://www.google.com/', proxies=proxy, auth=auth)
    print r.text
if __name__ == "__main__":
    proxy_test()

这篇stackoverflow文章将涵盖这个,以及请求的使用。下面是关于带有请求库的代理的更多信息。

看起来您有一个HTTP(不是HTTPS)代理。

代理的回答表明无法验证您的身份验证:HTTP Error 407: Proxy Authentication Required .

您可以尝试下面的代码。您可以在代理服务器返回的代理认证报头上检查代理领域。

proxy_handler = urllib2.ProxyHandler({'http': 'http://proxy.company.local:3128/'})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('Company Proxy Realm', 'proxy.company.local', 'username', 'password')
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
opener.open('http://www.google.com')
opener.open('https://www.google.com')
相关文章: