CherryNewb: can't get static content working

49 views
Skip to first unread message

ej

unread,
Oct 17, 2006, 4:33:22 PM10/17/06
to cherrypy-users
Hello! (my apologies if this is duplicate - previous email didn't seem
to go through)

I am just trying to get savvy with CherryPy and trying to work
through some of
the tutorials and create a simple application. I am trying to get a
directory set
up to serve static content out of, and can't seem to get it to work.
The example
I am trying to follow is here: http://cherrypy.org/wiki/StaticContent

My configuration file is as follows, which I think is funcitonally the
same as the
example.

[global]
server.socket_port = 8088
server.thread_pool = 10
server.environment = "production"
tools.sessions.on = True
# server.showTracebacks = True
# server.logToScreen = False

[/]
tools.staticdir.root = "/home/ej/proj/calibration_pages"

[/static]
tools.staticdir.on = True
tools.staticdir.dir = "static"


I'm confident my server is actually reading the file, because I can see
that
it starts on the port specified (as well as restarts if you edit the
file while its running) but when I try to call a url such as:
http://localhost:8088/static/login.css

the server prints out:

192.168.0.135 - - [17/Oct/2006:14:00:47] "GET /static/login.css
HTTP/1.1"
404 1065 "" "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;
rv:1.8.0.6)
Gecko/20060728 Firefox/1.5.0.6"

The file is actually there...
ej@mako:~/proj/calibration_pages> pwd
/home/ej/proj/calibration_pages
ej@mako:~/proj/calibration_pages> ls -ld static
drwxr-xr-x 2 ej users 80 2006-10-17 11:09 static
ej@mako:~/proj/calibration_pages> ls -l static
total 4
-rwxr-xr-x 1 ej users 1553 2006-09-12 18:26 login.css


So... I guess I am missing something, but I don't see it. Anyone see
what's
wrong here?

Thanks! ;)
-ej

Message has been deleted

Sutabi

unread,
Oct 20, 2006, 6:19:37 AM10/20/06
to cherrypy-users

ej

unread,
Oct 20, 2006, 2:00:40 PM10/20/06
to cherrypy-users
On Oct 20, 4:19 am, "Sutabi" <sut...@gmail.com> wrote:
> http://groups-beta.google.com/group/cherrypy-users/browse_thread/thre...


OK, well... either something is still not working right, or I am not
understanding something about the quickstart() method. I have an added
complication in that port 8080 is in use by Apache, so I need to
specify a different port to run on. For the record, I am running
CherryPy-3.0.0beta2

First of all, the following file (slightly modified from Christian's
example - no decorators in Python 2.3.4 ) does what I would expect
(that is, I can call URL's such as http://machine:8088/static/bar.txt
and get the file contents of anything in that directory served up (and
the given link from index works too)):

#-----------------------------------------------------------------------

#!/usr/bin/env python

import cherrypy

class MyFiles(object):
def index(self):
return """\
<html>
<head><title>foo.txt</title></head>
<body>
<a href='/static/foo.txt'>foo.txt</a>
</body>
</html>
"""
index.exposed = True


cherrypy.config.update(
{ 'server.socket_port' : 8088,
'tools.staticdir.root' : '/home/ej/proj/calibration_pages',
}
)

appconfig = {
'/static' : { 'tools.staticdir.on' : True,
'tools.staticdir.dir' : 'static',
},
}

cherrypy.quickstart(MyFiles(), config=appconfig)
# EOF
#-----------------------------------------------------------------------

Trying to go to Christian's example with a config file, this code does
not start a server on port 8088 as I would expect, and naturally dumps
a stack trace saying port 8080 is in use:

ej@mako:~/proj/calibration_pages> cat static2.conf
[global]
server.socket_port = 8088

[/static]
tools.staticdir.on = True
tools.staticdir.dir = "/home/ej/proj/calibration_pages/static"
ej@mako:~/proj/calibration_pages> cat static2.py
#!/usr/bin/env python

import cherrypy

class MyFiles(object):
def index(self):
return """\
<html>
<head><title>foo.txt</title></head>
<body>
<a href='/static/foo.txt'>foo.txt</a>
</body>
</html>
"""
index.exposed = True

# cherrypy.config.update('static2.conf')
# cherrypy.quickstart(MyFiles())

cherrypy.quickstart(MyFiles(), config='static2.conf')


ej@mako:~/proj/calibration_pages> ./static2.py
[20/Oct/2006:11:24:15] HTTP Port 8080 not free on 'localhost'
Traceback (most recent call last):
File "./static2.py", line 20, in ?
cherrypy.quickstart(MyFiles(), config='static2.conf')
File "/usr/home/ej/CherryPy-3.0.0beta2/cherrypy/__init__.py", line
28, in quickstart
server.quickstart()
File "/usr/home/ej/CherryPy-3.0.0beta2/cherrypy/_cpserver.py", line
69, in quickstart
self.start()
File "/usr/home/ej/CherryPy-3.0.0beta2/cherrypy/_cpserver.py", line
97, in start
self._start_http(httpserver)
File "/usr/home/ej/CherryPy-3.0.0beta2/cherrypy/_cpserver.py", line
106, in _start_http
wait_for_free_port(*bind_addr)
File "/usr/home/ej/CherryPy-3.0.0beta2/cherrypy/_cpserver.py", line
224, in wait_for_free_port
raise IOError(msg)
IOError: Port 8080 not free on 'localhost'
ej@mako:~/proj/calibration_pages>

OK, if I change the file to run the other two lines that were
previously commented...


ej@mako:~/proj/calibration_pages> vi static2.py
ej@mako:~/proj/calibration_pages> cat static2.py
#!/usr/bin/env python

import cherrypy

class MyFiles(object):
def index(self):
return """\
<html>
<head><title>foo.txt</title></head>
<body>
<a href='/static/foo.txt'>foo.txt</a>
</body>
</html>
"""
index.exposed = True

cherrypy.config.update('static2.conf')
cherrypy.quickstart(MyFiles())

# cherrypy.quickstart(MyFiles(), config='static2.conf')


The server now runs...

ej@mako:~/proj/calibration_pages> ./static2.py
[20/Oct/2006:11:25:44] HTTP Serving HTTP on http://localhost:8088/


But static content doesn't get served. More explicitly:

http://mako:8088/

begets:

192.168.0.135 - - [20/Oct/2006:11:27:37] "GET / HTTP/1.1" 200 114 ""


"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.6)
Gecko/20060728 Firefox/1.5.0.6"

that is, the web page (index) you would expect gets served, but if you
click on the link, the server prints this:

192.168.0.135 - - [20/Oct/2006:11:27:43] "GET /static/foo.txt HTTP/1.1"
404 1061 "http://mako:8088/" "Mozilla/5.0 (Windows; U; Windows NT 5.0;


en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6"

and you get a 404 error formatted in your browser.


So, first of all, it seems to me that these two lines:

cherrypy.config.update('static2.conf')
cherrypy.quickstart(MyFiles())

should be functionally equivalent to this line:

cherrypy.quickstart(MyFiles(), config='static2.conf')

They are not. Is it your expectation that they would be, or am I
misunderstanding something?

Christian's example without config file specifies
tools.staticdir.root and then specifies tools.staticdir.dir as a
relative path, but his example with a config file doesn't specify
tools.staticdir.root and then gives an absolute path for
tools.statdir.dir. This makes sense to me. So, given that those should
be equivalent, it is my expectation that this code:

cherrypy.config.update(
{ 'server.socket_port' : 8088,
'tools.staticdir.root' : '/home/ej/proj/calibration_pages',
}
)

appconfig = {
'/static' : { 'tools.staticdir.on' : True,
'tools.staticdir.dir' : 'static',
},
}

cherrypy.quickstart(MyFiles(), config=appconfig)


Should give the same behavior as this code:

cherrypy.config.update('static2.conf')
cherrypy.quickstart(MyFiles())

when static2.conf has this text:

[global]
server.socket_port = 8088

[/static]
tools.staticdir.on = True
tools.staticdir.dir = "/home/ej/proj/calibration_pages/static"


It does not. It seems to me that something is fishy in parsing and
using config files. Is it also your expectation that these would be
equivalent, or am I misunderstanding something?

Thanks for any contributed help,
-ej

Robert Brewer

unread,
Oct 20, 2006, 3:03:05 PM10/20/06
to cherryp...@googlegroups.com
ej wrote:
> it is my expectation that this code:
>
> cherrypy.config.update(
> { 'server.socket_port' : 8088,
> 'tools.staticdir.root' : '/home/ej/proj/calibration_pages',
> }
> )
>
> appconfig = {
> '/static' : { 'tools.staticdir.on' : True,
> 'tools.staticdir.dir' : 'static',
> },
> }
>
> cherrypy.quickstart(MyFiles(), config=appconfig)
>
>
> Should give the same behavior as this code:
>
> cherrypy.config.update('static2.conf')
> cherrypy.quickstart(MyFiles())
>
> when static2.conf has this text:
>
> [global]
> server.socket_port = 8088
>
> [/static]
> tools.staticdir.on = True
> tools.staticdir.dir = "/home/ej/proj/calibration_pages/static"
>
>
> It does not. It seems to me that something is fishy in parsing and
> using config files. Is it also your expectation that these would be
> equivalent, or am I misunderstanding something?

They are not equivalent, but I think you're right that they should be.

Index: __init__.py
===================================================================
--- __init__.py (revision 1401)
+++ __init__.py (working copy)
@@ -24,6 +24,7 @@

def quickstart(root, script_name="", config=None):
"""Mount the given app, start the engine and builtin server, then
block."""
+ _local_config.update(config)
tree.mount(root, script_name, config)
server.quickstart()
engine.start()
@@ -253,4 +254,4 @@

# Set up config last so it can wrap other top-level objects
from cherrypy import _cpconfig
-config = _cpconfig.Config()
+config = _local_config = _cpconfig.Config()


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

fumanchu

unread,
Oct 21, 2006, 2:35:19 PM10/21/06
to cherrypy-users
ej wrote:
> It seems to me that something is fishy in parsing and
> using config files. Is it also your expectation that these
> would be equivalent, or am I misunderstanding something?

and I answered:


> They are not equivalent, but I think you're right that they should be.

And now they are: http://www.cherrypy.org/changeset/1404.

Reply all
Reply to author
Forward
0 new messages