AWS - 运行web服务 - +的CherryPy的Python [英] AWS - Running webservice - Cherrypy + Python
问题描述
我有一个Linux机器(Ubuntu的10.10服务器版)的EC2。我已经写了使用CherryPy的框架的Web服务。比方说,这是我写的code。
进口SYS
sys.path.insert(0,'cherrypy.zip')
进口的CherryPy
从进口的CherryPy揭露
一流的服务:
@暴露
高清指数(个体经营):
返回的'Hello World
cherrypy.quickstart(服务())
我在EC2实例复制该文件,该文件cherrypy.zip在/ var / WWW。 [我要通知我手动创建www目录,因为它不存在。然后我跑
蟒蛇webservice.py
和得到的消息
[01 /四月/ 2011:13:50:04] ENGINE车开了
然而,当我尝试运行
(我掩盖我的公网IP)
ec2-1 ** - 2 ** - 1 ** - ** ap-southeast-1.compute.amazonaws.com/
在我的浏览器,我得到的连接失败。谁能告诉我,我已经错了?或者我应该怎么办?
编辑: 好吧,这里是一些有趣的事情,我发现。当我做
蟒蛇webservice.py
我看
ENGINE服务上127.0.0.1:8080
这意味着,web服务将只运行在本地机器。如何让我设置服务0.0.0.0(也就是服务于任何IP地址?)
希望这个细节足以理解我所面临的问题。帮助请:)
编辑2: 哦,找到了解决:-)。不得不cherrypy.quickstart()调用之前添加此
cherrypy.config.update({'server.socket_host:0.0.0.0,
server.socket_port':80,
})
在cherrypy.quickstart函数有一个配置参数,它可以是一个字典,一个开放的配置文件,或者路径到配置文件。我倾向于使用一个路径的配置文件,因为最小化的设置,您可能preFER从启动脚本来控制的硬编码。
此外,因为您可以控制的服务器上,可以配置一个反向代理将请求路由到CherryPy的应用程序。这给你相当多的灵活性。例如,如果你愿意,你可以并行运行的CherryPy应用程序的多个实例,每个实例配置到不同的端口上监听。
下面是nginx的一个示例配置文件,指示其将请求转发到您的CherryPy应用程序的一个实例:
服务器
{
SERVER_NAME your.hostname.com;
位置 / {
proxy_pass http://127.0.0.1:8080/;
}
}
这是一个示例配置文件指示nginx的负载平衡在您的应用程序的两个实例,这在端口33334和33335进行监听环回地址:
上游myapps {
服务器127.0.0.1:33334;
服务器127.0.0.1:33335;
}
服务器 {
SERVER_NAME your.hostname.com;
位置 / {
proxy_pass HTTP:// myapps;
}
}
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;
}
}
这篇关于AWS - 运行web服务 - +的CherryPy的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!