最佳答案
我有两个 Python 脚本,一个使用 Urllib2图书馆,一个使用 请求库。
我发现 Request 更容易实现,但是我找不到 urlib2的 read()
函数的等价物。例如:
...
response = url.urlopen(req)
print response.geturl()
print response.getcode()
data = response.read()
print data
一旦我建立了我的文章的网址,data = response.read()
给我的内容-我试图连接到一个 vcloud 总监 api 实例和响应显示我可以访问的端点。但是,如果我按照以下方式使用请求库... ..。
....
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"
....
... . print post_call.text
和 print post_call.content
不返回任何内容,即使请求后呼叫的状态码等于200。
为什么“请求”的响应没有返回任何文本或内容?