Skip to main content
 首页 » 编程设计

python之配置 cherrypy 以供全局使用

2025年05月04日7shangdawei

我正在尝试使用他们网站上的示例来测试 cherrypy 框架:

import cherrypy 
class HelloWorld(object): 
    def index(self): 
    return "Hello World!" 
    index.exposed = True 
 
cherrypy.quickstart(HelloWorld()) 

当我运行它时,我会在控制台中收到以下响应:

[05/Dec/2011:00:15:11] ENGINE Listening for SIGHUP. 
[05/Dec/2011:00:15:11] ENGINE Listening for SIGTERM. 
[05/Dec/2011:00:15:11] ENGINE Listening for SIGUSR1. 
[05/Dec/2011:00:15:11] ENGINE Bus STARTING 
CherryPy Checker: 
The Application mounted at '' has an empty config. 
 
[05/Dec/2011:00:15:11] ENGINE Started monitor thread '_TimeoutMonitor'. 
[05/Dec/2011:00:15:11] ENGINE Started monitor thread 'Autoreloader'. 
[05/Dec/2011:00:15:12] ENGINE Serving on 127.0.0.1:8080 
[05/Dec/2011:00:15:12] ENGINE Bus STARTED 

当在本地运行浏览器并指向 localhost:8080 时,它可以工作。我如何配置应用程序以响应域名:www.example.com?我希望 Hello World 在我的具有域名的生产服务器上进行测试,以便世界上任何人都可以从任何位置或任何计算机访问它?

请您参考如下方法:

您将为生产服务器使用静态 ip...

config = { 
  'global' : { 
    'server.socket_host' : 'XXX.XXX.XXX.XXX', 
    'server.socket_port' : 80, 
    } 
} 
 
cherrypy.quickstart(HelloWorld(), '/', config) 

然后为您的域 www.example.com 设置一个指向静态 ip 的 dns 条目。
希望这对您有所帮助!