Having trouble mounting multiple applications at different URLs

605 views
Skip to first unread message

Lorem Ipsum

unread,
Apr 2, 2015, 11:48:59 AM4/2/15
to cherryp...@googlegroups.com
Good day fellow CherryPyers,

I am learning to use CherryPy and making good progress, but I have hit a roadblock.

I am following the tutorials in the documentation, but I am using Nginx as a reverse-proxy to the CherryPy server.

I am not sure if I explained my problem well in the title. I am making an extremely simple website just to test the set-up, but I cannot get it to work correctly. I can successfully display a single application, but I cannot figure out how to host different applications at different URLs. I am getting this error:
TypeError: object of type 'website' has no len()

I am sure I am missing very simple but I cannot, for the life of me, figure it out.

These are the files in my root directory:

=========================
application_module.py
=========================

import cherrypy

class website(object):
    @cherrypy.expose
    def index(self):
        return ("The cute bunny hopped through the forest.")

    @cherrypy.expose
    def brown(self):
        return ("The brown bunny has very good luck.")


=========================
wsgi.py

=========================

import application_module

def application(env, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [application_module.website()]



=========================
server.py

=========================

from wsgi import application
import cherrypy

if __name__ == '__main__':

    cherrypy.tree.graft(application, "/")

    cherrypy.server.unsubscribe()

    server = cherrypy._cpserver.Server()

    server.socket_host = "0.0.0.0"
    server.socket_port = 8080
    server.thread_pool = 30

    server.subscribe()

    cherrypy.engine.start()
    cherrypy.engine.block()



=========================
and this is the Nginx configuration file:
=========================

worker_processes    1;

events {
    worker_connections  1024;
}

http {
    sendfile            on;

    gzip_http_version   1.0;
    gzip_proxied        any;
    gzip_min_length     500;
    gzip_disable        "MSIE [1-6]\.";
    gzip_types          text/plain text/xml text/css
                        text/javascript
                        application/javascript;

    upstream apps {
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
    }

    server {
       listen 80;
       # server_name  www.example.com
       server_name  localhost;
   
       # access_log  /app/logs/www.example.com.log combined;
       # error_log  /app/logs/www.example.com.log;
   
       location ^~ /static/  {
          root /home/user/cherrypy_website/static;
       }
   
       location / {
          proxy_pass         http://apps;
          proxy_redirect     off;
          proxy_set_header   Host $host;
          proxy_set_header   X-Real-IP $remote_addr;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Host $server_name;
       }
    }
}

Lorem Ipsum

unread,
Apr 2, 2015, 1:17:03 PM4/2/15
to cherryp...@googlegroups.com
I have figured it out! With the help of this article: http://ionrock.org/2014/02/24/a_sample_cherrypy_app_stub.html

If anyone runs into a similar problem in the future, all I did to fix this is change my server.py to:

import cherrypy
from application_module import website

def get_app():
    cherrypy.tree.mount(website(), '/')
    return cherrypy.tree


def start():
    get_app()
    cherrypy.engine.signals.subscribe()
    cherrypy.engine.start()
    cherrypy.engine.block()

if __name__ == '__main__':
    start()

Lorem Ipsum

unread,
Apr 2, 2015, 2:11:06 PM4/2/15
to cherryp...@googlegroups.com
Although really, I do not know why what I did fixed it. If anyone is feeling informative, I would love to hear why it worked! :)

Tim Roberts

unread,
Apr 2, 2015, 2:18:23 PM4/2/15
to cherryp...@googlegroups.com
Lorem Ipsum wrote:
Although really, I do not know why what I did fixed it. If anyone is feeling informative, I would love to hear why it worked! :)

Well, look at what you had in your WSGI module:


import application_module

def application(env, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [application_module.website()]


"website" is a plain, ordinary Python class.  CherryPy is not involved here in any way.  You just didn't connect the two together.  Your modification did so.
-- 
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
Reply all
Reply to author
Forward
0 new messages