简体   繁体   English

如何使用CherryPy配置IP地址?

[英]How do I configure the ip address with CherryPy?

I'm using python and CherryPy to create a simple internal website that about 2 people use. 我正在使用python和CherryPy创建一个大约2个人使用的简单内部网站。 I use the built in webserver with CherryPy.quickstart and never messed with the config files. 我将内置的Web服务器与CherryPy.quickstart一起使用,并且从未弄乱过配置文件。 I recently changed machines so I installed the latest Python and cherrypy and when I run the site I can access it from localhost:8080 but not through the IP or the windows machine name. 我最近更换了机器,所以我安装了最新的Python和cherrypy,当我运行该站点时,可以从localhost:8080访问它,但不能通过IP或Windows机器名访问它。 It could be a machine configuration difference or a newer version of CherryPy or Python. 可能是机器配置不同,也可能是CherryPy或Python的较新版本。 Any ideas how I can bind to the correct IP address? 有什么想法可以绑定到正确的IP地址吗?

Edit: to make it clear, I currently don't have a config file at all. 编辑:为了清楚起见,我目前根本没有配置文件。

server.socket_host: '0.0.0.0'

...would also work. ...也可以。 That's IPv4 INADDR_ANY, which means, "listen on all interfaces". 这就是IPv4 INADDR_ANY,这意味着“在所有接口上监听”。

In a config file, the syntax is: 在配置文件中,语法为:

[global]
server.socket_host: '0.0.0.0'

In code: 在代码中:

cherrypy.server.socket_host = '0.0.0.0'

That depends on how you are running the cherrypy init. 这取决于您如何运行cherrypy init。

If using cherrypy 3.1 syntax, that wold do it: 如果使用cherrypy 3.1语法,那么请这样做:

cherrypy.server.socket_host = 'www.machinename.com'
cherrypy.engine.start()
cherrypy.engine.block()

Of course you can have something more fancy, like subclassing the server class, or using config files. 当然,您可以拥有更多花哨的东西,例如对服务器类进行子类化或使用配置文件。 Those uses are covered in the documentation . 这些用途在文档中介绍

But that should be enough. 但这应该足够了。 If not just tell us what you are doing and cherrypy version, and I will edit this answer. 如果不只是告诉我们您在做什么和令人毛骨悚然的版本,我将编辑此答案。

import cherrypy

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

cherrypy.server.socket_host = '0.0.0.0' # put it here 
cherrypy.quickstart(HelloWorld())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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