Skip to content

Commit

Permalink
move cherrypy boilerplate to its own util function
Browse files Browse the repository at this point in the history
  • Loading branch information
plq committed Jul 16, 2015
1 parent b9a173a commit e25d69d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
44 changes: 3 additions & 41 deletions examples/cherry/wsgi.py
Expand Up @@ -39,6 +39,7 @@
"""


import sys
import logging

from spyne import Application, srpc, ServiceBase, Iterable, UnsignedInteger, \
Expand All @@ -47,6 +48,7 @@
from spyne.protocol.json import JsonDocument
from spyne.protocol.http import HttpRpc
from spyne.server.wsgi import WsgiApplication
from spyne.util.cherry import cherry_graft_and_start


class HelloWorldService(ServiceBase):
Expand Down Expand Up @@ -93,44 +95,4 @@ def say_hello(name, times):
# a ZeroMQ (REQ/REP) wrapper.
wsgi_application = WsgiApplication(application)

# Use Cherrypy as wsgi server.
# Source: https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-applications-using-a-cherrypy-web-server-behind-nginx
import cherrypy

# Mount the application
cherrypy.tree.graft(wsgi_application, "/")

# Unsubscribe the default server
cherrypy.server.unsubscribe()

# Instantiate a new server object
server = cherrypy._cpserver.Server()

# Configure the server object
server.socket_host = "0.0.0.0"
server.socket_port = 8080
server.thread_pool = 30

# For SSL Support
# server.ssl_module = 'pyopenssl'
# server.ssl_certificate = 'ssl/certificate.crt'
# server.ssl_private_key = 'ssl/private.key'
# server.ssl_certificate_chain = 'ssl/bundle.crt'

# Subscribe this server
server.subscribe()

# Example for a 2nd server (same steps as above):
# Remember to use a different port

# server2 = cherrypy._cpserver.Server()

# server2.socket_host = "0.0.0.0"
# server2.socket_port = 8081
# server2.thread_pool = 30
# server2.subscribe()

# Start the server engine (Option 1 *and* 2)

cherrypy.engine.start()
cherrypy.engine.block()
sys.exit(cherry_graft_and_start(wsgi_application))
41 changes: 41 additions & 0 deletions spyne/util/cherry.py
@@ -0,0 +1,41 @@
# Use Cherrypy as wsgi server.
# Source: https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-applications-using-a-cherrypy-web-server-behind-nginx

import logging
import cherrypy


def cherry_graft_and_start(wsgi_application, host="0.0.0.0", port=8080,
num_threads=30, ssl_module=None, cert=None, key=None, cacert=None):

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

# Mount the application
cherrypy.tree.graft(wsgi_application, "/")

# Unsubscribe the default server
cherrypy.server.unsubscribe()

# Instantiate a new server object
server = cherrypy._cpserver.Server()

# Configure the server object
server.socket_host = host
server.socket_port = port
server.thread_pool = num_threads

# For SSL Support
if ssl_module is not None:
server.ssl_module = ssl_module # eg. 'pyopenssl'
server.ssl_certificate = cert # eg. 'ssl/certificate.crt'
server.ssl_private_key = key # eg. 'ssl/private.key'
server.ssl_certificate_chain = cacert # eg. 'ssl/bundle.crt'

# Subscribe this server
server.subscribe()

# Start the server engine (Option 1 *and* 2)
cherrypy.engine.start()

return cherrypy.engine.block()

0 comments on commit e25d69d

Please sign in to comment.