Python 如何在suds 0.3.6中添加http头?

Python 如何在suds 0.3.6中添加http头?,python,http,header,suds,Python,Http,Header,Suds,我在Python2.5中有一个应用程序,它通过SUD0.3.6发送数据 问题是数据包含非ascii字符,因此我需要在soap消息中存在以下标头: Content Type=“text/html;charset=“utf-8” SOAP消息中存在的头只是: Content Type=“text/html” 我知道它在suds 0.4中是固定的,但它需要Python2.6,我需要Python2.5,因为我使用CentOS,它需要该版本。所以问题是: 如何更改或向SOAP消息添加新的HTTP头?在ur

我在Python2.5中有一个应用程序,它通过SUD0.3.6发送数据

问题是数据包含非ascii字符,因此我需要在soap消息中存在以下标头:

Content Type=“text/html;charset=“utf-8”

SOAP消息中存在的头只是:

Content Type=“text/html”

我知道它在suds 0.4中是固定的,但它需要Python2.6,我需要Python2.5,因为我使用CentOS,它需要该版本。所以问题是:


如何更改或向SOAP消息添加新的HTTP头?

在urllib2中创建opener时,可以使用一些处理程序执行任何操作。例如,如果要在suds中添加新头,应执行以下操作:

https = suds.transport.https.HttpTransport()
opener = urllib2.build_opener(HTTPSudsPreprocessor)
https.urlopener = opener
suds.client.Client(URL, transport = https)
class HTTPSudsPreprocessor(urllib2.BaseHandler):

    def http_request(self, req):
        req.add_header('Content-Type', 'text/xml; charset=utf-8')
        return req

    https_request = http_request
其中HTTPSudsPreprocessor是您自己的处理程序,它应该如下所示:

https = suds.transport.https.HttpTransport()
opener = urllib2.build_opener(HTTPSudsPreprocessor)
https.urlopener = opener
suds.client.Client(URL, transport = https)
class HTTPSudsPreprocessor(urllib2.BaseHandler):

    def http_request(self, req):
        req.add_header('Content-Type', 'text/xml; charset=utf-8')
        return req

    https_request = http_request
您必须重写的方法取决于您想要执行的操作。请至少在suds 0.4(可能更早?)中查看Python.org中的urllib2文档。HTTP头也可以传递给构造函数或通过
set\u options
方法:

client = suds.client.Client(url, headers={'key': 'value'})
client.set_options(headers={'key2': 'value'})

这是可行的,可能比定义一个新的处理程序只是为了添加头要好。我遇到了一个strnage错误405方法,这是不允许的