Python:显示为列表httplib.HTTPMessage时出现问题
我这里有个小问题。因此,我正在为一个著名的RESTAPI编写一些调用。一切都进行得很顺利,除了我希望所有的响应都显示为一个列表(这对我来说是更好的操作)。我的职能是:Python:显示为列表httplib.HTTPMessage时出现问题,python,api,rest,httplib,Python,Api,Rest,Httplib,我这里有个小问题。因此,我正在为一个著名的RESTAPI编写一些调用。一切都进行得很顺利,除了我希望所有的响应都显示为一个列表(这对我来说是更好的操作)。我的职能是: import sys, httplib HOST = "api.sugarsync.com" API_URL = "https://api.sugarsync.com" def do_request(xml_location): request = open(xml_location,"r").read()
import sys, httplib
HOST = "api.sugarsync.com"
API_URL = "https://api.sugarsync.com"
def do_request(xml_location):
request = open(xml_location,"r").read()
webservice = httplib.HTTPS(HOST)
webservice.putrequest("POST", "authorization", API_URL)
webservice.putheader("Host", HOST)
webservice.putheader("User-Agent","Python post")
webservice.putheader("Content-type", "application/xml")
webservice.putheader("Content-type", "application/xml")
webservice.putheader("Accept", "*/*")
webservice.putheader("Content-length", "%d" % len(request))
webservice.endheaders()
webservice.send(request)
statuscode, statusmessage, header = webservice.getreply()
result = webservice.getfile().read()
return statuscode, statusmessage, header
return result
do_request('C://Users/my_user/Documents/auth.xml')
我习惯于使用split(),但在这种情况下,结果是:
[201, 'Created', <httplib.HTTPMessage instance at 0x0000000001F68AC8>]
[201,‘已创建’,]
我还需要第三个对象(httplib.HTTPMessage实例,0x0000000001F68AC8>)显示为列表,以提取其中的一些数据
提前谢谢 httplib.HTTPMessage类似于dict,下面是一个示例:
import httplib
from cStringIO import StringIO
h = httplib.HTTPMessage(StringIO(""))
h["Content-Type"] = "text/plain"
h["Content-Length"] = "1234"
print h.items()
您只需调用它的函数items(),它将返回一个标题列表