如何使用pythonxmlrpclib为RPC调用发送自定义http廑u头?

2024-10-01 11:21:37 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用pythonxmlrpclib库发送自定义HTTP Headers? 我需要在调用RPC方法时发送一些特殊的自定义http_headers。在


Tags: 方法httprpcheaderspythonxmlrpclib
1条回答
网友
1楼 · 发布于 2024-10-01 11:21:37

您可以子类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

相关问题 更多 >