cherrypy with mod_python. port not free error

52 views
Skip to first unread message

sanket

unread,
Oct 7, 2008, 3:51:31 PM10/7/08
to cherrypy-users
Hello All,

I am quite new to cherrypy and mod_python. I have set up my
application to run behind apache 2 with mod_python.

I am using cherrypy 3.1.0, apache 2, and mod_python 3.3.1

Here is my configuration in httpd.conf

<VirtualHost *:80>
ServerName myapp.com
ServerAlias www.myapp.com
DocumentRoot /var/www/html
<Location "/">
PythonPath "sys.path+['/var/www/html']"
SetHandler python-program
PythonHandler cherrypy._cpmodpy::handler
PythonOption cherrypy.setup cpdeploy::serverless
PythonDebug On
</Location>
LogLevel warn
ErrorLog /var/www/html/dummy-host.example.com-error_log
CustomLog /var/www/html/dummy-host.example.com-access_log combined
</VirtualHost>

And here is the code of my application : cpdeploy.py

--------------------------------------------------------------------------------------------------------------------------------------------

#!python
"""Deployment script for CherryPy + Apache (or other front-end)."""

import os

import cherrypy

class Track(object):

def index(self):
return "Track index"
index.exposed = True

def test(self):
return "Track::test"
test.exposed = True

class Album(object):

def index(self):
return "Album index"
index.exposed = True

def test(self):
return "Album::test"
test.exposed = True

class Root(object):

album = Album()
track = Track()
def index(self):
return "Hello cherrypy with mod_python"
index.exposed = True

def test(self):
return "This is a test method"
test.exposed = True

root = Root()

def serverless():
"""Start with no server (for mod_python or other WSGI HTTP
servers).

You can also use this mode interactively:
>>> import cpdeploy
>>> cpdeploy.serverless()
"""
# Set up site-wide config. Do this first so that,
# if something goes wrong, we get a log.
cherrypy.config.update({
'log.screen': False,
'log.error_file': '/tmp/site.log',
'environment': 'production',
'request.show_tracebacks': False,
# Turn off signal handlers when CP does not control the OS
process
'engine.SIGTERM': None,
'engine.SIGHUP': None
})
cherrypy.tree.mount(root)
try:
cherrypy.engine.start()
except:
cherrypy.log(traceback=True)
raise

def serve():
"""Start with the builtin server."""
# Set up site-wide config. Do this first so that,
# if something goes wrong, we get a log.
cherrypy.config.update({
'log.screen': True,
'log.error_file': '/tmp/site.log',
'environment': 'production',
})
cherrypy.tree.mount(root)
#cherrypy.server.quickstart()
cherrypy.engine.start()

if __name__ == "__main__":
serve()
---------------------------------------------------------------------------------------------------------------------------------------------------

Now the problem is, it runs fine with very first request and on the
second request I get
Unrecoverable error in the server error. and here is the traceback
which is found in /tmp/site.log file.

File "/usr/lib/python2.4/site-packages/cherrypy/_cpmodpy.py",
line 139, in handler
setup(req)
File "/usr/lib/python2.4/site-packages/cherrypy/_cpmodpy.py",
line 84, in setup
func()
File "/var/www/html/cpdeploy.py", line 65, in serverless
cherrypy.engine.start()
File "/usr/lib/python2.4/site-packages/cherrypy/process/
wspbus.py", line 180, in start
self.publish('start')
File "/usr/lib/python2.4/site-packages/cherrypy/process/
wspbus.py", line 147, in publish
output.append(listener(*args, **kwargs))
File "/usr/lib/python2.4/site-packages/cherrypy/_cpserver.py",
line 89, in start
ServerAdapter.start(self)
File "/usr/lib/python2.4/site-packages/cherrypy/process/
servers.py", line 53, in start
wait_for_free_port(*self.bind_addr)
File "/usr/lib/python2.4/site-packages/cherrypy/process/
servers.py", line 209, in wait_for_free_port
raise IOError("Port %r not free on %r" % (port, host))
IOError: Port 8080 not free on '127.0.0.1'

-------------------------------------------------------------------------------------------------------------------------------------------

I am stuck on this for a while. I would really appreciate your help.

Thank you,
Sanket

Robert Brewer

unread,
Oct 7, 2008, 4:15:25 PM10/7/08
to cherryp...@googlegroups.com
sanket wrote:
> I am quite new to cherrypy and mod_python. I have set up my
> application to run behind apache 2 with mod_python.
>
> I am using cherrypy 3.1.0, apache 2, and mod_python 3.3.1
> Now the problem is, it runs fine with very first request and on the
> second request I get
> IOError: Port 8080 not free on '127.0.0.1'
>
> def serverless():
> cherrypy.config.update({
> 'log.screen': False,

> 'engine.SIGTERM': None,
> 'engine.SIGHUP': None
> })
> try:
> cherrypy.engine.start()
> except:
> cherrypy.log(traceback=True)
> raise

You don't need any of the above lines when using _cpmodpy.setup in
CherryPy 3.1--it will set those config entries and call engine.start for
you, and most importantly it will turn off cherrypy.server. By starting
the engine yourself before _cpmodpy.setup does, you're beating it to the
punch. Just take out those lines from your serverless function and leave
engine.start to _cpmodpy. See the source code for more details.


Robert Brewer
fuma...@aminus.org


sanket

unread,
Oct 7, 2008, 4:26:53 PM10/7/08
to cherrypy-users
Hi Robert,
Thanks a lot.

It worked. I really didn't know that things are bit different with
cherrypy 3.1.
All of the tutorials I followed were for older versions.

Thanks a lot again,
Sanket
> fuman...@aminus.org
Reply all
Reply to author
Forward
0 new messages