带有xml内容的重定向命中时,Python 2.7 urllib2提高urllib2.HTTPError 301 [英] Python 2.7 urllib2 raising urllib2.HTTPError 301 when hitting redirect with xml content

查看:98
本文介绍了带有xml内容的重定向命中时,Python 2.7 urllib2提高urllib2.HTTPError 301的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用urllib2在hxxp://s3.amazonaws.com/mybucket处请求特定的S3存储桶. Amazon发送回301代码的HTTP代码以及一些XML数据(重定向到hxxp://mybucket.s3.amazonaws.com/). python不执行重定向,而是引发urllib2.HTTPError: HTTP Error 301: Moved Permanently.

I'm using urllib2 to request a particular S3 bucket at hxxp://s3.amazonaws.com/mybucket. Amazon sends back an HTTP code of 301 along with some XML data (the redirect being to hxxp://mybucket.s3.amazonaws.com/). Instead of following the redirect, python raises urllib2.HTTPError: HTTP Error 301: Moved Permanently.

根据官方Python文档,网址为如何使用urllib2获取Internet资源 ,默认处理程序处理重定向(代码在300范围内)".

According to the official Python docs at HOWTO Fetch Internet Resources Using urllib2, "the default handlers handle redirects (codes in the 300 range)".

python是否处理错误(可能是由于响应中出现意外的XML),或者我做错了什么?我已经看过Wireshark,响应返回到python的请求与使用Web客户端对我的请求完全相同.在调试中,我看不到XML在响应对象中的任何地方捕获.

Is python handling this incorrectly (presumably because of the unexpected XML in the response), or am I doing something wrong? I've watched in Wireshark and the response comes back exactly the same to python's request as it does to me using a web client. In debugging, I don't see the XML being captured anywhere in the response object.

感谢您的指导.

对不起,最初没有发布代码.没什么特别的,实际上就是这个-

Sorry for not posting the code initially. It's nothing special, literally just this -

import urllib2, httplib

request = urllib2.Request(site)
response = urllib2.urlopen(request)

推荐答案

最好使用requests库. requests默认情况下处理重定向: http://docs.python-requests.org/en/latest/user/quickstart/#redirection-and-history

You are better off using the requests library. requests handle redirection by default : http://docs.python-requests.org/en/latest/user/quickstart/#redirection-and-history

import requests

response = requests.get(site)
print(response.content)

我没有遇到urllib2的问题,我试图查看文档 https://docs.python.org/2/library/urllib2.html ,但看起来并不直观.

I don't get the problem with urllib2, I tried to look into the documentation https://docs.python.org/2/library/urllib2.html but it doesn't look intuitive.

似乎在Python3中,他们对其进行了重构以减轻使用负担,但我仍然坚信requests是必经之路.

It seems that in Python3, they refactored it to make it less a burden to use, but I am still convinced that requests is the way to go.

注意urllib2模块已拆分为以下几个模块: Python 3名为urllib.request和urllib.error. 2to3工具将 在将源转换为Python 3时会自动调整导入.

Note The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

这篇关于带有xml内容的重定向命中时,Python 2.7 urllib2提高urllib2.HTTPError 301的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆