Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if __name__ == '__main__':
# Python daemon boilerplate
logging.basicConfig(level=logging.DEBUG)
# Instantiate the application by giving it:
# * The list of services it should wrap,
# * A namespace string.
# * An input protocol.
# * An output protocol.
application = Application([HelloWorldService], 'spyne.examples.hello.http',
# The input protocol is set as HttpRpc to make our service easy to
# call. Input validation via the 'soft' engine is enabled. (which is
# actually the the only validation method for HttpRpc.)
in_protocol=HttpRpc(validator='soft'),
# The ignore_wrappers parameter to JsonDocument simplifies the reponse
# dict by skipping outer response structures that are redundant when
# the client knows what object to expect.
out_protocol=JsonDocument(ignore_wrappers=True),
)
# Now that we have our application, we must wrap it inside a transport.
# In this case, we use Spyne's standard Wsgi wrapper. Spyne supports
# popular Http wrappers like Twisted, Django, Pyramid, etc. as well as
# a ZeroMQ (REQ/REP) wrapper.
wsgi_application = WsgiApplication(application)
sys.exit(cherry_graft_and_start(wsgi_application))
def _on_method_call(ctx):
ctx.udc = UserDefinedContext()
def _on_method_return_object(ctx):
ctx.udc.session.commit()
def _on_method_context_closed(ctx):
if ctx.udc is not None:
ctx.udc.session.close()
application = Application([TCrudService(User, 'user')],
tns='spyne.examples.sql_crud',
in_protocol=HttpRpc(validator='soft'),
out_protocol=YamlDocument())
application.event_manager.add_listener('method_call', _on_method_call)
application.event_manager.add_listener('method_return_object',
_on_method_return_object)
application.event_manager.add_listener("method_context_closed",
_on_method_context_closed)
if __name__=='__main__':
from wsgiref.simple_server import make_server
wsgi_app = WsgiApplication(application)
server = make_server('127.0.0.1', 8000, wsgi_app)
TableModel.Attributes.sqla_metadata.create_all()
class HelloWorldService(Service):
@rpc(_returns=Array(Permission))
def simple(ctx):
return v
@rpc(_returns=Permission.customize(max_occurs=float('inf')))
def complex(ctx):
return v
if __name__=='__main__':
from wsgiref.simple_server import make_server
logging.basicConfig(level=logging.DEBUG)
application = Application([HelloWorldService], 'spyne.examples.hello.http',
in_protocol=HttpRpc(validator='soft'),
out_protocol=XmlDocument(),
)
wsgi_application = WsgiApplication(application)
server = make_server('127.0.0.1', 8000, wsgi_application)
logging.info("listening to http://127.0.0.1:8000")
logging.info("wsdl is at: http://localhost:8000/?wsdl")
server.serve_forever()
def main():
# start publisher process
p = multiprocessing.Process(target=camera_publisher)
p.start()
stream_app = WsgiApplication(Application([StreamingService],
tns='spyne.examples.stream',
in_protocol=HttpRpc(),
out_protocol=HttpRpc(mime_type='video/ogg'),
))
root_app = DispatcherMiddleware(NotFound(), {'/stream': stream_app})
# get stream header from the publisher process
socket = context.socket(zmq.REQ)
socket.connect(header_socket)
socket.send("hey")
StreamingService.stream_header = socket.recv()
socket.close()
# have fun!
run_simple('0.0.0.0', port, root_app, static_files={'/':"."}, threaded=True)
def main():
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.wsgi').setLevel(logging.DEBUG)
filemgr_app = WsgiApplication(Application([FileServices],
tns='spyne.examples.file_manager',
in_protocol=HttpRpc(validator='soft'),
out_protocol=HttpRpc()
))
try:
os.makedirs('./files')
except OSError:
pass
wsgi_app = DispatcherMiddleware(NotFound(), {'/filemgr': filemgr_app})
logger.info("navigate to: http://localhost:9000/index.html")
return run_simple('localhost', port, wsgi_app, static_files={'/': 'static'},
threaded=True)
def create_app(flask_app):
"""Creates SOAP services application and distribute Flask config into
user con defined context for each method call.
"""
application = Application(
[HelloWorldService], 'spyne.examples.flask',
# The input protocol is set as HttpRpc to make our service easy to call.
in_protocol=HttpRpc(validator='soft'),
out_protocol=JsonDocument(ignore_wrappers=True),
)
# Use `method_call` hook to pass flask config to each service method
# context. But if you have any better ideas do it, make a pull request.
# NOTE. I refuse idea to wrap each call into Flask application context
# because in fact we inside Spyne app context, not the Flask one.
def _flask_config_context(ctx):
ctx.udc = UserDefinedContext(flask_app.config)
application.event_manager.add_listener('method_call', _flask_config_context)
return application
@classmethod
def dispatch(cls):
application = Application([cls], tns=cls.__tns__, in_protocol=HttpRpc(validator="soft"), out_protocol=JsonP())
return application
@rpc(_returns=Array(User))
def list_users(ctx):
global user_database
return user_database.values()
if __name__ == '__main__':
from wsgiref.simple_server import make_server
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
application = Application([UserManager], 'spyne.examples.complex',
in_protocol=HttpRpc(), out_protocol=XmlDocument())
server = make_server('127.0.0.1', 8000, WsgiApplication(application))
logging.info("listening to http://127.0.0.1:8000")
logging.info("wsdl is at: http://localhost:8000/?wsdl")
server.serve_forever()
def initialize(services, tns='spyne.examples.twisted.resource'):
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
observer = log.PythonLoggingObserver('twisted')
log.startLoggingWithObserver(observer.emit, setStdout=False)
return Application(services, 'spyne.examples.twisted.hello',
in_protocol=HttpRpc(), out_protocol=HttpRpc())
def main():
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
services = (SomeService, SomeAuxService)
application = Application(services, 'spyne.examples.auxproc',
in_protocol=HttpRpc(), out_protocol=XmlDocument())
server = make_server(host, port, WsgiApplication(application))
logging.info("listening to http://%s:%d" % (host, port))
return server.serve_forever()