How do I configure the ip address with CherryPy?
Asked Answered
S

3

20

I'm using python and CherryPy to create a simple internal website that about 2 people use. I use the built in webserver with CherryPy.quickstart and never messed with the config files. 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. It could be a machine configuration difference or a newer version of CherryPy or Python. Any ideas how I can bind to the correct IP address?

Edit: to make it clear, I currently don't have a config file at all.

Saucepan answered 22/9, 2008 at 16:4 Comment(0)
B
18

That depends on how you are running the cherrypy init.

If using cherrypy 3.1 syntax, that wold do it:

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.

Barramunda answered 22/9, 2008 at 16:16 Comment(3)
I just needed the first line before my call to cherrypy.quickstart, thanksSaucepan
the link is brokenVerb
on "using config files" -- note that cherrypy.quickstart(application(), config = configfile) can take an optional config file as the second argument (e.g. in the cherrypy tutorial it is tutconf), which as far as I know will override the cherrypy.server.socket_host set in the programRanders
L
40
server.socket_host: '0.0.0.0'

...would also work. That's IPv4 INADDR_ANY, which means, "listen on all interfaces".

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'
Losel answered 30/9, 2008 at 6:55 Comment(3)
Either. Edited the above to reflect that.Losel
Why would you ever need to listen on all interfaces? Could you explain a bit more here?Unfavorable
very common in docker containers.Brownley
B
18

That depends on how you are running the cherrypy init.

If using cherrypy 3.1 syntax, that wold do it:

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.

Barramunda answered 22/9, 2008 at 16:16 Comment(3)
I just needed the first line before my call to cherrypy.quickstart, thanksSaucepan
the link is brokenVerb
on "using config files" -- note that cherrypy.quickstart(application(), config = configfile) can take an optional config file as the second argument (e.g. in the cherrypy tutorial it is tutconf), which as far as I know will override the cherrypy.server.socket_host set in the programRanders
C
9
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())
Chumash answered 28/12, 2016 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.