4

我这里有个小问题。所以,我正在为一个众所周知的 REST API 编写一些调用。一切都很顺利,除了我希望所有响应都显示为列表(这对我来说更好操作)。我的功能是这样的:

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>]

好吧,我还需要第三个对象(httplib.HTTPMessage 实例位于 0x0000000001F68AC8>),以列表形式显示,以提取其中的一些数据。

提前致谢!

4

1 回答 1

1

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(),它将返回一个标题列表

于 2012-03-12T18:38:14.390 回答