Setup Differnt Port for CherryPy

450 views
Skip to first unread message

Will Addison

unread,
Apr 13, 2016, 5:09:39 PM4/13/16
to cherrypy-users
Hello,

I am having trouble with setting CherryPy to a different port. I'm new to Python and CherryPy. I purchased a book called Python 3 Web Development and I've been trying to go by that but I'm lost. Currently Port 8080 is being used by something else in the book it says you can change the port but it doesn't explain that well how to do that or where to do that. Can someone help me?

This is the original code for the exercise.
import cherrypy
import os.path
current_dir = os.path.dirname(os.path.abspath(__file__))
class Root(object):
 
 content = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" href="static/css/smoothness/jquery-ui-1.8.4.custom.css" type="text/css" media="screen, projection" />
<script type="text/javascript" src="static/jquery-1.4.2.js" ></script>
<script type="text/javascript" src="static/jquery-ui-1.8.4.custom.min.js" ></script>
</head>
<body id="spreadsheet_example">
<div id="example">
 an empty div
</div>
</body>
</html>
 '''
 
 @cherrypy.expose
 def index(self):
  return Root.content
 
if __name__ == "__main__":
 
 cherrypy.quickstart(Root(),config={
  '/static':
  { 'tools.staticdir.on':True,
    'tools.staticdir.dir':current_dir+"/static"
  }
 })

The book says the following and this is where I'm confused.

If we would like CherryPy to listen on a different port, we should indicate this in

the global configuration. This can be done by preceding the call to cherrypy.

quickstart() with cherrypy.config.update({'server.socket_

port':8088}). CherryPy has a rich palette of configuration options and can

even be instructed to read its configuration from files. A good starting point for

all the possibilities is http://www.cherrypy.org/wiki/ConfigAPI.


Nicklas Börjesson

unread,
Apr 24, 2016, 3:23:06 AM4/24/16
to cherrypy-users
Not sure what the question is or what is confusing you, are you saying that doesn't work?
Anyway, the relevant documentation is at:

Michiel Overtoom

unread,
Apr 24, 2016, 7:28:15 AM4/24/16
to cherryp...@googlegroups.com
Hi Will,

You wrote:

> I am having trouble with setting CherryPy to a different port.

and the book said:

> If we would like CherryPy to listen on a different port, we should indicate this in the global configuration. This can be done by preceding the call to cherrypy.quickstart() with cherrypy.config.update({'server.socket_port':8088}).


So, your code becomes:


import cherrypy
import os.path

current_dir = os.path.dirname(os.path.abspath(__file__))

class Root(object):
content = "<p>Hello there</p>"

@cherrypy.expose
def index(self):
return Root.content


if __name__ == "__main__":
cherrypy.config.update({
"server.socket_port": 8088,
})
config = {
"/static": {
"tools.staticdir.on": True,
"tools.staticdir.dir": current_dir + "/static",
}
}
cherrypy.quickstart(Root(), "/", config)


Greetings,

Reply all
Reply to author
Forward
0 new messages