如何在泡沫0.3.6中添加http标头? [英] How to add http headers in suds 0.3.6?

查看:85
本文介绍了如何在泡沫0.3.6中添加http标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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



,并且SOAP消息中存在的头只是:

Content-Type =text / html



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

我怎样才能改变或添加新的HTTP标头到SOAP消息?

解决方案

在urllib2中创建opener时,可以使用一些处理程序来执行任何你想要的操作。例如,如果你想在suds中添加一个新的头文件,你应该这样做:

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

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

  class HTTPSudsPreprocessor urllib2.BaseHandler):

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

https_request = http_request

你必须重写的方法取决于你想要做什么。请参阅Python.org中的urllib2文档。


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

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"

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

Content-Type="text/html"

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:

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

解决方案

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)

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

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

这篇关于如何在泡沫0.3.6中添加http标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆