Search code examples
spyne

Error running spyne example on Windows


I tried running a spyne example. This same example works on Linux but on Windows gives following error-

  File "helloWorld.py", line 14, in <module>
    class HelloWorldService(ServiceBase):
  File "helloWorld.py", line 15, in HelloWorldService
    @srpc(Unicode, Integer, _returns=Iterable(Unicode))
NameError: name 'srpc' is not defined

Does anyone have a solution??


Solution

  • That example is wrong. Here's the correct version:

    import logging
    logging.basicConfig(level=logging.DEBUG)
    
    from spyne import Application, rpc, ServiceBase, \
        Integer, Unicode
    
    from spyne import Iterable
    
    from spyne.protocol.http import HttpRpc
    from spyne.protocol.json import JsonDocument
    
    from spyne.server.wsgi import WsgiApplication
    
    
    class HelloWorldService(ServiceBase):
        @rpc(Unicode, Integer, _returns=Iterable(Unicode))
        def say_hello(ctx, name, times):
            for i in range(times):
                yield 'Hello, %s' % name
    
    
    application = Application([HelloWorldService],
        tns='spyne.examples.hello',
        in_protocol=HttpRpc(validator='soft'),
        out_protocol=JsonDocument()
    )
    
    
    if __name__ == '__main__':
        # You can use any Wsgi server. Here, we chose
        # Python's built-in wsgi server but you're not
        # supposed to use it in production.
        from wsgiref.simple_server import make_server
        wsgi_app = WsgiApplication(application)
        server = make_server('0.0.0.0', 8000, wsgi_app)
        server.serve_forever()
    

    I also fixed the code on http://spyne.io. Sorry about that and thanks a lot for bringing the problem to my attention.