公司代理在C#中工作,但在Python中不工作

时间:2014-07-17 08:19:32

标签: c# python proxy

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

如果您不绝对需要使用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帖子将涵盖此内容,使用requests.Session个对象Here是有关请求lib的代理的更多信息。希望这对你来说更容易。

答案 2 :(得分:1)

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

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

您可以尝试以下代码。您可以在代理服务器返回的Proxy-Authentication标头上检查代理域。

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')