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

さて、そこにあるデータの一部を抽出するために、リストとして表示される 3 番目のオブジェクト (0x0000000001F68AC8> の httplib.HTTPMessage インスタンス) も必要です。

前もって感謝します!

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 に答える