2012-03-12 70 views
2

我在這裏有一個小問題。所以,我正在撰寫一些關於衆所周知的REST API的調用。一切都進展順利,除了我希望所有響應都以列表形式顯示(這對我來說更好)。我的功能是這樣的:Python:顯示爲列表問題httplib.HTTPMessage

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>)被顯示爲列表,以提取其中的一些數據。

在此先感謝!

回答

0

httplib.HTTPMessage有點像字典,這裏是一個示例:

import httplib 
from cStringIO import StringIO 

h = httplib.HTTPMessage(StringIO("")) 
h["Content-Type"] = "text/plain" 
h["Content-Length"] = "1234" 

print h.items() 

你只需要調用它的功能項目(),它將返回頭

列表