卷曲示例到pycurl

时间:2016-01-08 05:51:44

标签: python curl pycurl

这是我的命令

curl -i -H 'X-Storage-User:12345:12345' -H 'X-Storage-Pass:new1234' https://ssproxy.ucloudbiz.olleh.com/auth/v1.0

结果

HTTP/1.1 200 OK
Date: Fri, 08 Jan 2016 05:49:06 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 126
Connection: close
X-Auth-Token-Expires: 51114
X-Auth-Token: AUTH_tkdfefef8dfsfsefsf
X-Storage-Token: AUTH_tkdfefef8dfsfsefsf
X-Storage-Url: https://ssproxy.ucloudbiz.olleh.com/v1/AUTH_3xxsdff_sdfwef_sdfwf
X-Trans-Id: tx76sdsefwfwwt

如何将其转换为pycurl?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

以下是用Python 3编写的pycurl中的相应命令:

import pycurl
import sys

def bodycb(buf):
    import sys
    sys.stdout.buffer.write(buf)

def headercb(buf):
    import sys
    sys.stdout.buffer.write(buf)

headers = ['X-Storage-User:12345:12345', 'X-Storage-Pass:new1234']

pycurl_connect = pycurl.Curl()
pycurl_connect.setopt(pycurl.URL, 'https://ssproxy.ucloudbiz.olleh.com/auth/v1.0')
pycurl_connect.setopt(pycurl.HTTPHEADER, headers)
pycurl_connect.setopt(pycurl.WRITEFUNCTION, bodycb)
pycurl_connect.setopt(pycurl.HEADERFUNCTION, headercb)
pycurl_connect.perform() 

您需要使用pycurl.HTTPHEADER传入列表中的标题。 headercb和bodycb函数只是将响应标题和正文打印到控制台的回调。