How to add http headers in suds 0.3.6?(如何在 suds 0.3.6 中添加 http 标头?)
问题描述
我在 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
这篇关于如何在 suds 0.3.6 中添加 http 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 suds 0.3.6 中添加 http 标头?


基础教程推荐
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01