Esa*_*abe 6 python header http suds
我在python 2.5中有一个应用程序,它通过suds 0.3.6发送数据.
问题是数据包含非ascii字符,因此我需要在soap消息中存在以下标题:
Content-Type ="text/html; charset ="utf-8"
并且SOAP消息中存在的标题只是:
内容类型= "text/html的"
我知道它固定在suds 0.4中,但它需要Python2.6并且我需要Python2.5,因为我使用CentOS并且它需要该版本.所以问题是:
如何更改或添加新的HTTP标头到SOAP消息?
小智 13
至少在suds 0.4(可能更早?)HTTP标头也可以传递给构造函数或通过set_options方法:
client = suds.client.Client(url, headers={'key': 'value'})
client.set_options(headers={'key2': 'value'})
Run Code Online (Sandbox Code Playgroud)
当您在 urllib2 中创建 opener 时,您可以使用一些处理程序来执行您想要的操作。例如,如果您想在 suds 中添加新标头,您应该执行以下操作:
https = suds.transport.https.HttpTransport()
opener = urllib2.build_opener(HTTPSudsPreprocessor)
https.urlopener = opener
suds.client.Client(URL, transport = https)
Run Code Online (Sandbox Code Playgroud)
其中 HTTPSudsPreprocessor 是您自己的处理程序,它应该如下所示:
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
Run Code Online (Sandbox Code Playgroud)
您必须重写的方法取决于您想要执行的操作。请参阅 Python.org 中的 urllib2 文档