python urllib2下载文件 是否成功,Python urllib2如何知道我们的授权是否成功fu

当尝试访问未授权的URL时,Python的urllib2模块会抛出HTTPError 401异常。这个错误表示服务器需要有效的身份验证凭证。示例中,尽管设置了基本认证头,但因为使用了错误的用户名或密码导致授权失败。成功授权后,将收到一个响应对象作为返回。
摘要由CSDN通过智能技术生成

当您未获得授权并返回401状态时,将引发urllib2.HTTPError异常:>>> import urllib2, base64

>>> req = urllib2.Request('http://httpbin.org/basic-auth/foouser/barpw')

>>> base64string = base64.encodestring("%s:%s" % ("username", "password")).replace("\n", "")

>>> req.add_header("Authorization", "Basic %s" % base64string)

>>> urllib2.urlopen(req)

Traceback (most recent call last):

File "", line 1, in

File "/Users/mpietre/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 127, in urlopen

return _opener.open(url, data, timeout)

File "/Users/mpietre/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 410, in open

response = meth(req, response)

File "/Users/mpietre/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 523, in http_response

'http', request, response, code, msg, hdrs)

File "/Users/mpietre/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 448, in error

return self._call_chain(*args)

File "/Users/mpietre/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 382, in _call_chain

result = func(*args)

File "/Users/mpietre/Development/Library/buildout.python/parts/opt/lib/python2.7/urllib2.py", line 531, in http_error_default

raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)

urllib2.HTTPError: HTTP Error 401: UNAUTHORIZED

成功授权后,您将得到一个响应对象:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Python 自带的 urllib 库来下载文件。可以使用 urllib.request.urlretrieve() 方法来下载文件。 以下是一个示例代码: ```python import urllib.request url = 'http://www.example.com/file.txt' filename = 'file.txt' urllib.request.urlretrieve(url, filename) ``` 在上面的代码中,url 是要下载文件的 URL,filename 是保存该文件的路径和文件名。 如果你需要下载文件需要身份验证,可以使用 urllib.request.HTTPBasicAuthHandler() 方法来添加身份验证信息。以下是一个示例代码: ```python import urllib.request import urllib.error url = 'http://www.example.com/file.txt' filename = 'file.txt' username = 'your_username' password = 'your_password' password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, username, password) auth_handler = urllib.request.HTTPBasicAuthHandler(password_mgr) opener = urllib.request.build_opener(auth_handler) try: urllib.request.install_opener(opener) urllib.request.urlretrieve(url, filename) except urllib.error.URLError as e: print(e.reason) ``` 在上面的代码中,我们使用了 HTTPPasswordMgrWithDefaultRealm() 方法来创建密码管理器,添加了我们的用户名和密码。然后使用 HTTPBasicAuthHandler() 方法来创建身份验证处理器。使用 build_opener() 方法来创建 opener 对象,使我们能够使用身份验证访问 URL。最后使用 install_opener() 方法来安装 opener 对象。如果下载过程中出现错误,我们打印错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值