Change Cherrypy Port and restart web server
Asked Answered
C

2

11

Is there a way in python to change the port that cherrypy is using and force the web server (not apache, cherrypy) to restart?

Cynthea answered 31/8, 2011 at 9:8 Comment(0)
I
21

Have a look at cherrypy.process.servers. You can try something like this:

import cherrypy
cherrypy.config.update({'server.socket_port': 8099})
cherrypy.engine.restart()
Iso answered 31/8, 2011 at 9:16 Comment(2)
While I agree this should be the correct answer, I found that simply doing a config update and then calling restart on my server caused it to get 'stuck' with the messages: "Waiting for child threads to terminate..." and "Waiting for thread Thread-1.". Not sure why this would be.Hashim
restart() appears to be not thread-safe, whereas stop() and start() are, at least in my environment where I run cp in a separate thread.Bayreuth
P
9

If you don't want to replace the whole process (which is what cherrypy.engine.restart() does), you could do:

import cherrypy
cherrypy.engine.stop()
cherrypy.server.httpserver = None
cherrypy.config.update({'server.socket_port': 8099})
cherrypy.engine.start()

Setting httpserver to None is needed or the cherrypy.engine.start() call will just reuse the host/port it already has rather than picking up the changed configuration. I'm not sure if that is inappropriately taking advantage of an implementation detail, though.

Parrett answered 20/8, 2014 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.