为了传输数据,我在命令行中输入以下内容:
curl -X POST -H "Authorization: Bearer {my_access_token}"
https://example1.example2.com/example3/example4/example5/example6
我明白了:
<data>
<sessionid>{sessionid}</sessionid>
<url>https://example1.example2.com/example3/example4/
example5</url>
</data>
然后,我输入:
curl -X POST -d "person=Adam&sessionid={sessionid}"
https://example6.example2.com/example3/example4/example5
作为回应,我的数据流在终端中连续输出。
但是,我想使用Python以编程方式提出相同的请求。我使用请求库。我有以下代码:
import httplib
import urllib
import requests
person = "Adam"
connection = httplib.HTTPSConnection('example1.example2.com', 443,
timeout
= 30)
# Headers
headers = {"Accept":"application/json",
"Authorization":"Bearer {my_access_token}"
# Send synchronously
connection.request('POST', '/example3/example4/example5/example6',
None,
headers)
#Connection for streaming here
try:
response = connection.getresponse()
content = response.read()
# Success
# print('Response status ' + str(response.status))
stringOption = content.decode('UTF-8')
json_objOption = json.loads(stringOption)
# Put the data into a text file
with open('data.txt', 'w') as outfile:
json.dump(json_objOption, outfile)
sessionid = json_objOption["data"]["sessionid"]
except httplib.HTTPException, e:
# Exception
print('Exception during request')
httplib.HTTPConnection._http_vsn = 10
httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
API_ENDPOINT =
"https://example6.example2.com/example3/example4/example5"
headers = {"Accept":"application/json"}
data = {'person': person,
'sessionid': sessionid}
r = requests.post(url = API_ENDPOINT, headers = headers, data =
data)
print(r.text)
print(r)
print(r.status_code)
print(r.headers)
没有任何打印语句输出任何内容。运行此脚本后没有输出。为什么会这样?