How to add http headers in suds 0.3.6?

后端 未结 2 588
逝去的感伤
逝去的感伤 2021-01-05 10:56

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 t

相关标签:
2条回答
  • 2021-01-05 11:48

    At least in suds 0.4 (maybe earlier?) HTTP headers can also be passed in to the constructor or via the set_options method:

    client = suds.client.Client(url, headers={'key': 'value'})
    client.set_options(headers={'key2': 'value'})
    
    0 讨论(0)
  • 2021-01-05 11:54

    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 讨论(0)
提交回复
热议问题