我有两个 Python 脚本。一个使用的是urllib2库一个使用请求库.
我发现 Requests 更容易实现,但我找不到 urlib2 的等效项read()
功能。例如:
...
response = url.urlopen(req)
print response.geturl()
print response.getcode()
data = response.read()
print data
一旦我建立了我的帖子网址,data = response.read()
为我提供内容 - 我正在尝试连接到 vclouddirector api 实例,响应显示我有权访问的端点。但是,如果我按如下方式使用 Requests 库......
....
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
即使请求后调用中的状态代码等于 200,也不会返回任何内容。
为什么我的请求响应没有返回任何文本或内容?