如何从 Python 请求中读取响应? [英] How do I read a response from Python Requests?
问题描述
我有两个 Python 脚本.一种使用 Urllib2 库,另一种使用 请求库.
我发现 Requests 更容易实现,但我找不到 urlib2 的 read()
函数的等效项.例如:
<代码>...响应 = url.urlopen(req)打印 response.geturl()打印 response.getcode()数据 = response.read()打印数据
一旦我建立了我的帖子 url,data = response.read()
就会给我内容 - 我正在尝试连接到 vcloud Director api 实例并且响应显示了我的端点可使用.但是,如果我使用 Requests 库如下.....
<代码>....def post_call(用户名,组织,密码,密钥,秘密):端点 = ''post_url = 端点 + '会话'get_url = 端点 + 'org'headers = {'Accept':'application/*+xml;version=5.1', \'授权':'基本'+ base64.b64encode(用户名+@"+组织+:"+密码),\'x-id-sec':base64.b64encode(key + ":" + secret)}打印标题post_call = requests.post(post_url, data=None, headers = headers)打印 post_call, "POST 调用"打印 post_call.text, "TEXT"打印 post_call.content, "内容"post_call.status_code, "状态代码"....
....print post_call.text
和 print post_call.content
不返回任何内容,即使请求后调用中的状态代码等于 200.
为什么我的请求响应没有返回任何文本或内容?
Requests 没有等效于 Urlib2 的 read()
.
看起来您发出的 POST 请求没有返回任何内容.POST 请求通常就是这种情况.也许它设置了一个cookie?状态码告诉你 POST 毕竟成功了.
针对 Python 3 进行
Python 现在以不同的方式处理数据类型.response.content
返回一个 bytes
(代表 ASCII 的整数)的序列,而 response.text
是一个 string
(字符序列).
因此,
>>>打印 response.content == response.text错误的>>>打印 str(response.content) == response.text真的
I have two Python scripts. One uses the Urllib2 library and one uses the Requests library.
I have found Requests easier to implement, but I can't find an equivalent for urlib2's read()
function. For example:
...
response = url.urlopen(req)
print response.geturl()
print response.getcode()
data = response.read()
print data
Once I have built up my post url, data = response.read()
gives me the content - I am trying to connect to a vcloud director api instance and the response shows the endpoints that I have access to. However if I use the Requests library as follows.....
....
def post_call(username, org, password, key, secret):
endpoint = '<URL ENDPOINT>'
post_url = endpoint + 'sessions'
get_url = endpoint + 'org'
headers = {'Accept':'application/*+xml;version=5.1', \
'Authorization':'Basic '+ base64.b64encode(username + "@" + org + ":" + password), \
'x-id-sec':base64.b64encode(key + ":" + secret)}
print headers
post_call = requests.post(post_url, data=None, headers = headers)
print post_call, "POST call"
print post_call.text, "TEXT"
print post_call.content, "CONTENT"
post_call.status_code, "STATUS CODE"
....
....the print post_call.text
and print post_call.content
returns nothing, even though the status code equals 200 in the requests post call.
Why isn't my response from Requests returning any text or content?
Requests doesn't have an equivalent to Urlib2's read()
.
>>> import requests
>>> response = requests.get("http://www.google.com")
>>> print response.content
'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"><head>....'
>>> print response.content == response.text
True
It looks like the POST request you are making is returning no content. Which is often the case with a POST request. Perhaps it set a cookie? The status code is telling you that the POST succeeded after all.
Edit for Python 3:
Python now handles data types differently. response.content
returns a sequence of bytes
(integers that represent ASCII) while response.text
is a string
(sequence of chars).
Thus,
>>> print response.content == response.text
False
>>> print str(response.content) == response.text
True
这篇关于如何从 Python 请求中读取响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!