我试图用python做一些jsonrpc调用。但是,由于api调用需要cookie,因此我创建了自己的传输类,jsonrpclib将使用它。这与最新的2.7 python完全一致。但是,当我尝试在python 2.6.6上运行它时,它抱怨" HTTP实例没有属性getresponse"
代码
class TransportMixIn(object):
""" Just extends the XMLRPC transport where necessary. """
user_agent = jsonrpclib.config.user_agent
# for Python 2.7 support
_connection = None
def send_content(self, connection, request_body):
connection.putheader("Content-Type", "application/json-rpc")
connection.putheader("Content-Length", str(len(request_body)))
if cookie != None :
connection.putheader("Cookie",cookie)
connection.endheaders()
if request_body:
connection.send(request_body)
def getparser(self):
target = JSONTarget()
return JSONParser(target), target
def request(self, host, handler, request_body, verbose=False):
# issue XML-RPC request
self.verbose = verbose
try:
h=self.make_connection(host)
if verbose:
h.set_debuglevel(1)
self.send_request(h,handler,request_body)
self.send_host(h,host)
self.send_user_agent(h)
self.send_content(h,request_body)
**resp = h.getresponse()**
if resp.status == 200:
self.verbose = verbose
global cookie
cookie = resp.getheader('set-cookie')
return self.parse_response(resp)
except xmlrpclib.Fault:
raise
except Exception:
#All unexpected errors leave connection in
# a strange state, so we clear it.
self.close()
raise
#We got an error response.
#Discard any response data and raise exception
if resp.getheader("content-length", ""):
resp.read()
raise xmlrpclib.ProtocolError(
host + handler,
resp.status, resp.reason,
dict(resp.getheaders())
)
class Transport(TransportMixIn, XMLTransport):
pass
class SafeTransport(TransportMixIn, XMLSafeTransport):
pass
错误发生在resp = h.getresponse()
的行任何想法? 顺便说一句,python是一种语言,当在不同版本上运行时,代码很容易搞砸?这种有线行为多次发生在我身上。