Spyne in_protocol Soap11和http标头

时间:2016-03-15 09:20:41

标签: nginx http-headers spyne

我无法在Spyne中处理http和https标头。 我有NginX + Twisted + Spyne,它运行良好,但我需要在Spyne中获得userId for filter功能。可能我应该去别的地方挖一下?

代码是: 客户端:

url_service = 'http://localhost:8000/?wsdl'
client = suds.client.Client(url_service)
client.set_options(headers={'ee': 'we'}, username = 'login')
w = client.service.get_head('neo')
print w

服务器:

import logging
import random
import sys
import base64

from spyne.application import Application
from spyne.decorator import rpc
from spyne.error import ResourceNotFoundError
from spyne.model.complex import ComplexModel
from spyne.model.fault import Fault
from spyne.model.primitive import Mandatory
from spyne.model.primitive import String
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
from spyne.service import ServiceBase

from twisted.internet import reactor 
from twisted.web.server import Site 
from twisted.web.wsgi import WSGIResource 
from twisted.python import log 

class RequestHeader(ComplexModel):
    __tns__ = 'spyne.examples.authentication'
    username = unicode()
    ee = unicode()

class UserService(ServiceBase):
    __tns__ = 'spyne.examples.authentication'
    __in_header__ = RequestHeader

    @rpc(Mandatory.String, _returns=String)
    def get_head(ctx, user_name):
        print '*'*20
        print ctx.in_header_doc
        print ctx.in_body_doc
        print ctx.in_header.ee
        retval = "Where's the header"
        return retval

def _on_method_call(ctx):
    return

UserService.event_manager.add_listener('method_call', _on_method_call)

HOST = '127.0.0.1' 
PORT = 8000 

if __name__=='__main__':
    from spyne.util.wsgi_wrapper import run_twisted

    logging.basicConfig(level=logging.DEBUG)
    logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
    logging.getLogger('twisted').setLevel(logging.DEBUG)

    application = Application([UserService],
        tns='spyne.examples.authentication',
        in_protocol=Soap11(validator='lxml'),
        out_protocol=Soap11()
    )

    wsgi_app = WsgiApplication(application)

    resource = WSGIResource(reactor, reactor, wsgi_app) 
    site = Site(resource) 
    reactor.listenTCP(PORT, site, interface=HOST) 

    logging.info("listening to http://127.0.0.1:8000")
    logging.info("wsdl is at: http://localhost:8000/?wsdl")
    sys.exit(reactor.run())

错误:

ERROR:spyne.application.server:'NoneType' object has no attribute 'ee'

Thanx in Advance,Yury

1 个答案:

答案 0 :(得分:1)

当您的in_protocol是SOAP时,ctx.in_headers用于soap标头。如果要检查HTTP标头,实际上需要查看传输标头。

由于每种运输工具都有自己的处理方式,因此这些运输工具主要是运输方式。对于Twisted HTTP,您需要从Twisted的HTTP包中获取Request对象。

您可以使用ctx.transport.req来抓住该对象。

至于从中提取标题,您需要查阅Twisted文档。

以下是为方便起见的相关页面:

https://twistedmatrix.com/documents/current/api/twisted.web.http.Request.html