Python 通过ISA 代理访问互联网报错 HTTP Error 407

urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied.  )

代码如下:

#!/python
# coding = utf-8  
from urllib import urlencode
import cookielib, urllib2

webURL = "http://www.google.com"

# prepare the cookie
cj = cookielib.LWPCookieJar();
cookie_support = urllib2.HTTPCookieProcessor(cj)


# set the proxy server
proxy_info = {
              'user' : 'scmroad',
              'pass' : 'pass',
              'host' : '192.168.1.101',
              'port' :  9876
              }
proxy_support = urllib2 . ProxyHandler ( {'http':\
                                          'http://%(user)s:%(pass)s@%(host)s:%(port)d' % proxy_info})

# construct the opener
opener = urllib2. build_opener(cookie_support, proxy_support)
urllib2.install_opener(opener)

page = urllib2.urlopen(webURL)
print page.read(1000)
page.close()


出错信息如下:
C:\script\webpy>python test.py
Traceback (most recent call last):
  File "test.py", line 27, in <module>
    page = urllib2.urlopen(webURL)
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 398, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 511, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 436, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 370, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 519, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied.  )
C:\script\webpy>


python urllib2 代理无效分析-NTLM

查了许久用urllib2通过代理访问网络的贴子,也写了测试代码,但是一直failed.Error Code: 407 Proxy Authentication Required. The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. (12209)
最后打算放弃用变通的方法现实现.
用curl库,网上找了下有对应python的接口----(pycurl)
马上down下,安装测试
测试code:

#coding=utf-8

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://www.ifeng.com')
import StringIO
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)
c.setopt(pycurl.PROXY, 'http://10.10.10.10:8080')
c.setopt(pycurl.PROXYUSERPWD, 'username:password')
c.perform()
print b.getvalue()


得到的结果还是:Error Code: 407 Proxy Authentication Required.
突然想起公司的OA都要加入域用代理才能上网。
又google下,搜到以下东东:


NTLM 身份验证
在网络环境中,NTLM (NT LAN Manager)用作身份验证协议以处理两台计算机(其中至少有一台计算机运行 Windows NT 4.0 或更早版本)之间的事务。具有此配置的网络称为“混合模式”,这是 Windows Server 2003 家族中的默认设置。
例如,以下配置将使用 NTLM 作为身份验证机制:
Windows 2000 或 Windows XP Professional 客户端向 Windows NT 4.0 的域控制器验证身份。
Windows NT 4.0 Workstation 客户端向 Windows 2000 或 Windows Server 2003 域控制器验证身份。
Windows NT 4.0 Workstation 客户端向 Windows NT 4.0 域控制器验证身份。
Windows NT 4.0 域中的用户向 Windows 2000 或运行 Windows Server 2003 家族的域验证身份。
运行向任何域控制器验证身份的 Windows 95、Windows 98 或 Windows Millennium Edition 的客户端。
另外,NTLM 是为没有加入到域中的计算机(如独立服务器和工作组)提供的身份验证协议。




分析后加上:c.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_NTLM)。功能OK。真是曲折啊。
不过还有其它好多认证方式,以后用代理不行,可以换个方向用其它认证试试。



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值