我有两个Python脚本。一种使用Urllib2库,另一种使用请求库。
我发现请求更易于实现,但是找不到urlib2的等效read()
函数。例如:
...
response = url.urlopen(req)
print response.geturl()
print response.getcode()
data = response.read()
print data
建立完发布网址后,请data = response.read()
提供内容-我正尝试连接到vcloud Director 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"
....
.... the print post_call.text
和不print post_call.content
返回任何内容,即使请求后调用中的状态代码等于200。
为什么我对请求的响应不返回任何文本或内容?
请求不具有与Urlib2等效的请求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
您发出的POST请求似乎不返回任何内容。POST请求通常是这种情况。也许它设置了一个cookie?状态代码告诉您POST毕竟成功。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句