2
如何使用python xmlrpclib
庫發送自定義HTTP Headers
庫! ? 我需要在調用RPC
方法時發送一些特殊的自定義http_headers
。如何使用python xmlrpclib爲RPC調用發送自定義http_headers?
如何使用python xmlrpclib
庫發送自定義HTTP Headers
庫! ? 我需要在調用RPC
方法時發送一些特殊的自定義http_headers
。如何使用python xmlrpclib爲RPC調用發送自定義http_headers?
您可以繼承xmlrpclib.Transport
並將其作爲參數傳遞給ServerProxy
。選擇一種方法來覆蓋(我選擇了send_content
)並設置好了。
# simple test program (from the XML-RPC specification)
from xmlrpclib import ServerProxy, Transport, Error
class SpecialTransport(Transport):
def send_content(self, connection, request_body):
print "Add your headers here!"
connection.putheader("Content-Type", "text/xml")
connection.putheader("Content-Length", str(len(request_body)))
connection.endheaders()
if request_body:
connection.send(request_body)
# server = ServerProxy("http://localhost:8000") # local server
server = ServerProxy("http://betty.userland.com", transport=SpecialTransport())
print server
try:
print server.examples.getStateName(41)
except Error, v:
print "ERROR", v
我不能夠使用這個,因爲我得到一個例外,自動對焦的第一個方法調用,見http://stackoverflow.com/questions/17569519/unable-to-use-custom-transport-class-用的Python-的xmlrpclib,由於對類型錯誤,UNB – sorin 2013-07-10 11:31:08