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

2022-01-11 00:00:00 python http header suds

问题描述

我在 python 2.5 中有一个应用程序,它通过 suds 0.3.6 发送数据.

I have an application in python 2.5 which sends data through suds 0.3.6.

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

The problem is that the data contains non-ascii characters, so I need the following header to exist in the soap message:

Content-Type="text/html; charset="utf-8"

而 SOAP 消息中存在的标头只是:

and the header that exists in the SOAP message is just:

Content-Type="text/html"

我知道它在 suds 0.4 中已修复,但它需要 Python2.6,我需要 Python2.5,因为我使用 CentOS,它需要那个版本.所以问题是:

I know that it is fixed in suds 0.4, but it requires Python2.6 and I NEED Python2.5 because I use CentOS and it needs that version. So the question is:

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

How could I change or add new HTTP headers to a SOAP message?


解决方案

当你在 urllib2 中创建 opener 时,你可以使用一些处理程序来做任何你想做的事情.例如,如果你想在 suds 中添加一个新的 header,你应该这样做:

When you create the opener in urllib2, you can use some handlers to do whatever you want. For example, if you want to add a new header in suds, you should do something like this:

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

HTTPSudsPreprocessor 是您自己的处理程序,它应该如下所示:

where HTTPSudsPreprocessor is your own handler, and it should look like this:

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

您必须覆盖的方法取决于您想要做什么.请参阅 Python.org 中的 urllib2 文档

The methods you have to override depend on what you want to do. See urllib2 documentation in Python.org

相关文章