簡體   English   中英

如何使用python xmlrpclib為RPC調用發送自定義http_headers?

[英]How to send custom http_headers for RPC calls using python xmlrpclib?

如何使用python xmlrpclib庫發送自定義HTTP Headers 我需要在調用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM