AWS - 运行网络服务 - Cherrypy + Python
我在 ec2 中有一个 Linux 盒子(Ubuntu 10.10 服务器版)。我使用cherrypy框架编写了一个Web服务。假设这是我编写的代码。
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())
我已将此文件(cherrypy.zip 文件)复制到我的 ec2 实例中的 /var/www 中。 [我应该通知我手动创建了 www 目录,因为它不在那里]。然后我运行
python webservice.py
并收到消息
[01/Apr/2011:13:50:04] ENGINE Bus STARTED
但是,当我尝试
(I have masked my public ip)
ec2-1**-2**-1**-**.ap-southeast-1.compute.amazonaws.com/
在浏览器中运行时,连接失败。谁能告诉我哪里出了问题?或者我应该做什么?
编辑: 好吧,这是我发现的一些有趣的事情。当我这样做时,
python webservice.py
我看到
ENGINE Serving on 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,
})
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,
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cherrypy.quickstart 函数接受一个 config 参数,该参数可以是一个字典、一个打开的配置文件或一个配置文件的路径。我喜欢使用配置文件的路径,因为这可以最大限度地减少您可能更喜欢从启动脚本控制的设置的硬编码。
此外,由于您控制服务器,因此您可以配置反向代理以将请求路由到 CherryPy 应用程序。这给了你相当大的灵活性。例如,如果您愿意,可以并行运行 CherryPy 应用程序的多个实例,每个实例配置为侦听不同的端口。
这是 nginx 的示例配置文件,指示它将请求转发到 CherryPy 应用程序的单个实例:
这是一个示例配置文件,指示 nginx 在应用程序的两个实例之间进行负载平衡,这两个实例正在侦听端口的环回地址33334 和 33335:
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:
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: