简体   繁体   中英

AWS - Running webservice - Cherrypy + Python

I have a linux box (Ubuntu 10.10 server edition) in ec2. I have written a web service using cherrypy framework. Let's say this is the code that I have written.

import sys
sys.path.insert(0,'cherrypy.zip')
import cherrypy
from cherrypy import expose

class Service:

    @expose
    def index(self):
        return 'Hello World'

cherrypy.quickstart(Service())

I have copied this file, the cherrypy.zip file to /var/www in my ec2 instance. [I should inform that I created the www directory manually, as it wasn't there]. Then I ran

python webservice.py

and got the message

[01/Apr/2011:13:50:04] ENGINE Bus STARTED

However, when I try to run

(I have masked my public ip)
ec2-1**-2**-1**-**.ap-southeast-1.compute.amazonaws.com/

in my browser, I get connection failed. Can anyone tell me where I have gone wrong? or what I should do?

EDIT: Okay, here is something interesting that I found. When I do

python webservice.py

I see

ENGINE Serving on 127.0.0.1:8080

Which means, the webservice will run only for the local machine. How do I make set the service 0.0.0.0 (that is, to serve any IP address?)

Hope this detail is sufficient for understanding the problem I'm facing. Help, please :)

EDIT 2: Oh well, found the solution :-) Have to add this before cherrypy.quickstart() call

cherrypy.config.update({'server.socket_host': '0.0.0.0',
                        'server.socket_port': 80,
                       })

The cherrypy.quickstart function takes a config argument, which can be a dict, an open configuration file, or a path to a configuration file. I favor using a path to a configuration file because that minimizes the hardcoding of settings that you might prefer to control from a startup script.

In addition, since you control the server, you could configure a reverse proxy to route requests to the CherryPy application. This gives you quite a bit of flexibility. For example, if you wanted to, you could run multiple instances of the CherryPy application in parallel, each configured to listen on a different port.

Here's a sample configuration file for nginx, instructing it to forward requests to a single instance of your CherryPy application:

server
{
  server_name your.hostname.com;

  location / {
    proxy_pass http://127.0.0.1:8080/;
  }
}

And here's a sample configuration file instructing nginx to load-balance across two instances of your application, which are listening on the loopback address at ports 33334 and 33335:

upstream myapps  {
  server 127.0.0.1:33334;
  server 127.0.0.1:33335;
}

server {
  server_name your.hostname.com;

  location / {
    proxy_pass  http://myapps;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM