`
darkjune
  • 浏览: 300975 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

python url2lib HTTP Error 407

阅读更多

公司的网络环境是通过代理上网,用python url2lib普通的代理验证不能通过,示例代码:

url = 'www.python.org'
username = 'user'
password = 'pass'
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# None, with the "WithDefaultRealm" password manager means
# that the user/pass will be used for any realm (where
# there isn't a more specific match).
password_mgr.add_password(None, url, username, password)
auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
print urllib2.urlopen("http://www.python.org")

 

仍然会报以下错误:

HTTP Error 407: Proxy Authentication Required ( Forefront TMG requires authorization to fulfill the request.

查了一下,发现跟公司网络代理使用NTLM验证有关,需要安装python ntml包来完成验证。

简单直接用 easy_install python-ntlm

安装好后下面是官方简单的连接代码:

import urllib2
from ntlm import HTTPNtlmAuthHandler

user = 'domain\user'
password = "pass"
url = "http://www.python.org"

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
# create the NTLM authentication handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)

# create and install the opener
opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)

# retrieve the result
response = urllib2.urlopen(url)
print(response.read())

 一次性成功。

 

1
2
分享到:
评论

相关推荐

    Python检测网站链接是否已存在

    Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。 Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。 像Perl语言一样, Python 源代码...整个程序引用了两个lib库,urllib2和sg

    对python3 urllib包与http包的使用详解

    urllib包和http包都是面向HTTP协议的。其中urllib主要用于处理 URL,使用...2.urllib.error ———定义了常见的urllib.request会引发的异常; 3.urllib.parse———用于解析 URL; 具体方法: urllib.request.urlop

    Python2.X/Python3.X中urllib库区别讲解

    Python2.X Python3.X urllib urllib.request, urllib.error, urllib.parse urllib2 urllib.request, urllib.error urllib2.urlopen urllib.request.urlopen urllib.urlencode urllib.parse.urlencode ...

    Python爬虫之urllib库

    文章目录1、urllib库介绍2、request模块3、error模块4、parse模块 1、urllib库介绍   urllib库是Python内置的请求库,能够实现简单的页面爬取功能。值得注意的是,在Python2中,有urllib和urllib2两个库来实现请求...

    解决python3 urllib中urlopen报错的问题

    最近更新了Python版本,准备写个爬虫,意外的发现urllib库中属性不存在urlopen,于是各种google,然后总结一下给出解决方案 问题的出现 AttributeError: ‘module’ object has no attribute ‘urlopen’ 问题的...

    python爬虫:请求页面基本库(一)urllib

    文章目录python爬虫:基本库(一)urllib使用urllib发送请求1.urlopen()添加data(附加数据)参数添加timeout(超时时间)参数2.Request()Request参数高级用法1.验证2.代理3.Cookies从网站中获取Cookies保存Cookies处理...

    Python3如何对urllib和urllib2进行重构

    python3对urllib和urllib2进行了重构,拆分成了urllib.request,urllib.response, urllib.parse, urllib.error等几个子模块,这样的架构从逻辑和结构上说更加合理。urllib库无需安装,python3自带。python 3.x中将...

    我的爬虫学习之路——urllib(urllib2整合到了urllib)

    urllib2在python3后已经合并在urllib中了,具体为urllib.response,urllib.request urllib2.URLError 改为了urllib.error.URLError 文章目录URLError与HTTPError下载网页(requests库也可以) URLError与HTTPError ...

    Python2/3中urllib库的一些常见用法

    Urllib是Python提供的一个用于操作URL的模块,我们爬取网页的时候,经常需要用到这个库。 升级合并后,模块中的包的位置变化的地方较多。 urllib库对照速查表 Python2.X Python3.X urllib urllib....

    Python urllib.request对象案例解析

    刚刚接触爬虫,基础的东西得时时回顾才行...2. urllib.error 异常处理模块 3. urllib.parse url 解析模块 4. urllib.robotparser robots.txt 解析模块 Urllib 库下的几种模块基本使用如下: urllib.request 关于 urlli

    零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers

    urlopen返回的应答对象response(或者HTTPError实例)有两个很有用的方法info()和geturl() 1.geturl(): 这个返回获取的真实的URL,这个很有用,因为urlopen(或者opener对象使用的)或许会有重定向。获取的URL或许跟...

    Python爬虫之urllib基础用法教程

    request : 它是最基本的 HTTP 请求模块,我们可以用它来模拟发送一请求,就像在浏览器里输入网址然后敲击回车一样,只需要给库方法传入 URL 还有额外的参数,就可以模拟实现这个过程了。 error : 异常处理模块,...

    python爬虫开发之urllib模块详细使用方法与实例全解

    在Pytho2.x中使用import urllib2——-对应的,在Python3.x中会使用import urllib.request,urllib.error 在Pytho2.x中使用import urllib——-对应的,在Python3.x中会使用import urllib.request,urllib.error,...

    Python爬虫中urllib库的进阶学习

    error的异常操作 urllib库除了以上基础的用法外,还有很多高级的功能,可以更加灵活的适用在爬虫应用中,比如: 使用HTTP的POST请求方法向服务器提交数据实现用户登录 使用代理IP解决防止反爬 设置超时提高爬虫...

    python3 中使用urllib问题以及urllib详解

    AttributeError: module ‘urllib’ has no attribute ‘request’ 查了一下资料是 python3 的 urllib 不会自动导入其under层的包,需要手动导入。 import urllib import urllib.parse import urllib.request ...

    安卓4.0源码编译问题

    File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner self.run() File "/usr/lib/python2.6/threading.py", line 484, in run self.__target(*self.__args, **self.__kwargs) File "/home...

    sqlmap (懂的入)

    python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --cookie "COOKIE_VALUE" 7、通过代理注入 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --proxy ...

    freemarker总结

    2,使用+运算符时,如果一边是数字,一边是字符串,就会自动将数字转换为字符串再连接,如:${3 + "5"},结果是:35 使用内建的int函数可对数值取整,如: ${ (x/2)?int } ${ 1.1?int } ${ 1.999?int } ${ -1.1?int } ...

Global site tag (gtag.js) - Google Analytics