How to tell Cherrypy to use any available port?

87 views
Skip to first unread message

raghu

unread,
Jan 15, 2007, 9:58:33 AM1/15/07
to cherrypy-users

Hi,

I know that a specific port can be given to cherrypy using
"server.socket_port" but I am wondering if there is a way to tell it to
pick up any available port (say, by passing 0 to the above option). I
tried this with 3.0RC1 but it tried to bind to 0 literally.

Any help is appreciated,
Raghu.

Christian Wyglendowski

unread,
Jan 15, 2007, 12:21:46 PM1/15/07
to cherryp...@googlegroups.com
So you would like CP to use any randomly open port between 0 and
65535? That sounds like functionality that you will have to add on
your own. What would be the use case for binding to a random port?

Christian

Sylvain Hellegouarch

unread,
Jan 15, 2007, 12:27:54 PM1/15/07
to cherryp...@googlegroups.com
Christian Wyglendowski wrote:
>
> So you would like CP to use any randomly open port between 0 and
> 65535? That sounds like functionality that you will have to add on
> your own. What would be the use case for binding to a random port?
>

This can be handy when you share the system with others that run the
same application and allow for a safe callback in case your port is taken.

In any case this is a fairly handy feature, not critical but interesting
IMO :)

- Sylvain

raghu

unread,
Jan 15, 2007, 12:38:59 PM1/15/07
to cherrypy-users

Christian Wyglendowski wrote:
> So you would like CP to use any randomly open port between 0 and
> 65535? That sounds like functionality that you will have to add on
> your own. What would be the use case for binding to a random port?

I have a script that parses a log file and then starts cherrypy.
Another thread in the same script simply calls "webbrowser <url>" where
"url" will be processed by started cherrypy server. So the end result
is a command like this will bring up a nice browser interface to my log
file.

$ logutil.py -f <logfile> -p <port>

Right now, I will have to explicitly pass different ports if I run this
simultaneously. Since the consumer of the web page is internal to the
script itself, it will be nice if I can tell cherrypy to use whatever
port is available. If I can then query the port that is used to bind, I
can use that in my webbrowser invocation.

Basically, I am asking for something like this:

Python 2.4.2 (#1, May 2 2006, 08:13:46)
[GCC 4.1.0 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> s = socket.socket()
>>> s.bind(("localhost", 0))
>>> s.getsockname()
('127.0.0.1', 40315)

Christian Wyglendowski

unread,
Jan 16, 2007, 10:23:14 AM1/16/07
to cherryp...@googlegroups.com
> Christian Wyglendowski wrote:
> > So you would like CP to use any randomly open port between 0 and
> > 65535? That sounds like functionality that you will have to add on
> > your own. What would be the use case for binding to a random port?

On 1/15/07, raghu <drag...@gmail.com> wrote:
> I have a script that parses a log file and then starts cherrypy.
> Another thread in the same script simply calls "webbrowser <url>" where
> "url" will be processed by started cherrypy server.

[snip]

> Since the consumer of the web page is internal to the
> script itself, it will be nice if I can tell cherrypy to use whatever
> port is available. If I can then query the port that is used to bind, I
> can use that in my webbrowser invocation.

Ah, that makes complete sense. Maybe using the random stdlib module
to select a random port number and then using
cherrypy._cpserver.check_port would help?

Christian

Robert Brewer

unread,
Jan 16, 2007, 12:16:50 PM1/16/07
to cherryp...@googlegroups.com

It seems to me like wsgiserver should allow a port of 0 to mean "any
available port". But a quick look through the code doesn't explain why
that doesn't already work (since we don't even use the sa from
getaddrinfo for bind(); we use self.bind_addr instead).

Raghu, please open a ticket.


Robert Brewer
System Architect
Amor Ministries
fuma...@amor.org

raghu

unread,
Jan 16, 2007, 12:45:14 PM1/16/07
to cherrypy-users
As it happens, I am right now looking at that part of the code
(wsgiserver/__init__.py). I could verify that port 0 does bind to a
random port as expected. But the problem I guess is that other parts of
code still use "configured" port which in this case is 0. For example,
my test script throws this exception:

-------------------------
File
"/localhome/raghu/localwork/cherrypy/svn.cherrypy.org/trunk/cherrypy/_cpserver.py",
line 69, in quickstart
self.start()
File
"/localhome/raghu/localwork/cherrypy/svn.cherrypy.org/trunk/cherrypy/_cpserver.py",
line 95, in start
self._start_http(httpserver)
File
"/localhome/raghu/localwork/cherrypy/svn.cherrypy.org/trunk/cherrypy/_cpserver.py",
line 118, in _start_http
self.wait(httpserver)
File
"/localhome/raghu/localwork/cherrypy/svn.cherrypy.org/trunk/cherrypy/_cpserver.py",
line 159, in wait
wait_for_occupied_port(*bind_addr)
File
"/localhome/raghu/localwork/cherrypy/svn.cherrypy.org/trunk/cherrypy/_cpserver.py",
line 247, in wait_for_occupied_port
raise IOError(msg)
IOError: Port 0 not bound on 'localhost'
-------------------------------------

I am trying to see if I can update self.bind_addr and if it makes any
difference. I am not very familiar with CP internals so this is a good
chance.

I will open a ticket.

Thanks,
Raghu.

Reply all
Reply to author
Forward
0 new messages