2011-01-07 29 views

回答

10

您可以繼承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 
+0

我不能夠使用這個,因爲我得到一個例外,自動對焦的第一個方法調用,見http://stackoverflow.com/questions/17569519/unable-to-use-custom-transport-class-用的Python-的xmlrpclib,由於對類型錯誤,UNB – sorin 2013-07-10 11:31:08

相關問題