gpt4 book ai didi

c# - Corporate Proxy 在 C# 中有效,但在 Python 中无效

转载 作者:行者123 更新时间:2023-11-28 22:48:43 24 4
gpt4 key购买 nike

我在公司代理后面,我正在尝试使用 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 代码有什么问题?

最佳答案

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

关于c# - Corporate Proxy 在 C# 中有效,但在 Python 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24798304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com