【发布时间】:2017-05-24 11:44:02
【问题描述】:
我正在玩弄数字海洋 api,我希望能够发送 HTTP 请求并在 python 中得到响应。但是,我不断收到 301 永久移动。当我使用curl 时,它工作正常。但是,当检查实际的 HTTP 标头时,看起来我正在发送相同的请求,但用户代理除外。谁能告诉我我做错了什么?
我使用的curl 命令是
curl -v -X GET "https://api.digitalocean.com/v2/actions" \
-H "Authorization: Bearer secret"
这是我的python代码。
import httplib
token = 'secret'
hostname = 'api.digitalocean.com'
conn = httplib.HTTPConnection(hostname)
conn.set_debuglevel(1)
conn.connect()
conn.putrequest('GET', '/v2/actions', skip_accept_encoding=True)
conn.putheader('Accept', '*/*')
conn.putheader('User-Agent', 'TestProgram')
conn.putheader('Authorization', 'Bearer ' + token)
conn.endheaders()
conn.send('')
response = conn.getresponse()
print response.read()
print response.getheaders()
conn.close()
【问题讨论】:
标签: python digital-ocean