0

我无法在 Spyne 中处理 http 和 https 标头。我有 NginX + Twisted + Spyne,它运行良好,但我需要在 Spyne 中获取 userId 以实现过滤功能。也许我应该在其他地方挖掘?

代码是:客户端:

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'

提前感谢,尤里

4

1 回答 1

1

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

由于每种交通工具都有自己的处理方式,因此这些方式主要是特定于交通工具的。对于 Twisted HTTP,您需要从 Twisted 的 HTTP 包中获取请求对象。

您可以使用ctx.transport.req来获取该对象。

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

为了您的方便,这里是相关页面:

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

于 2016-03-16T12:26:49.853 回答