AWS - 运行网络服务 - Cherrypy + Python

发布于 2024-10-28 13:26:24 字数 1170 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来日方长 2024-11-04 13:26:24

cherrypy.quickstart 函数接受一个 config 参数,该参数可以是一个字典、一个打开的配置文件或一个配置文件的路径。我喜欢使用配置文件的路径,因为这可以最大限度地减少您可能更喜欢从启动脚本控制的设置的硬编码。

此外,由于您控制服务器,因此您可以配置反向代理以将请求路由到 CherryPy 应用程序。这给了你相当大的灵活性。例如,如果您愿意,可以并行运行 CherryPy 应用程序的多个实例,每个实例配置为侦听不同的端口。

这是 nginx 的示例配置文件,指示它将请求转发到 CherryPy 应用程序的单个实例:

server
{
  server_name your.hostname.com;

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

这是一个示例配置文件,指示 nginx 在应用程序的两个实例之间进行负载平衡,这两个实例正在侦听端口的环回地址33334 和 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 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;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文