here is a WORKING example of an image served by cherrypy

1,071 views
Skip to first unread message

Eur van Andel

unread,
Nov 14, 2008, 5:21:07 AM11/14/08
to cherryp...@googlegroups.com
Hi

There seems to be much confusion about images, their directories and how to serve static files. 
The static content page of the Wiki should give 5 different examples, not one. Also, the example should be the bare minimum.

Anyway, this works:

import os.path
import cherrypy

class Root:
    @cherrypy.expose
    def index(self):
        return """ <html>
<head>
<title>CherryPy static test</title>
</head>
<html>
<body>
<p>Testing images from image dir</p>
<img src="images/today.png" width="640" height="480" border="0">
</body>
</html>"""

if __name__ == '__main__':
    current_dir = os.path.dirname(os.path.abspath(__file__))
    # Set up site-wide config first so we get a log if errors occur.
    cherrypy.config.update({'server.socket_host': '10.0.0.212',
                            'server.socket_port': 80, })

    conf = {'/images': {'tools.staticdir.on': True,
                      'tools.staticdir.dir': os.path.join(current_dir, 'images')}}

    cherrypy.quickstart(Root(), '/', config=conf)

where the image "today.png" is in the /images directory of the directory where the python file is. 

This works under FreeBSD 6.3, Cherrypy 3.1




---

ir EE van Andel e...@fiwihex.nl  http://www.fiwihex.nl

Fiwihex B.V. Wierdensestraat 74, NL7604BK Almelo, Netherlands

tel+31-546-491106 fax+31-546-491107



Eur van Andel

unread,
Nov 14, 2008, 6:20:58 AM11/14/08
to cherrypy-users


On Nov 14, 11:21 am, Eur van Andel <e...@fiwihex.nl> wrote:

> Anyway, this works:
> [...]

Here is an even smaller example:


import cherrypy

class Root:
@cherrypy.expose
def index(self):
return """ <html>
<head>
<title>CherryPy static tutorial</title>
</head>
<html>
<body>
<p>Testing images from image dir</p>
<img src="images/today.png" width="640" height="480" border="0">
</body>
</html>"""

cherrypy.config.update({'server.socket_host': '10.0.0.212',
'server.socket_port': 80, })

conf = {'/images': {'tools.staticdir.on': True,
'tools.staticdir.dir': '/home/eur/cherrypy/images'}}
cherrypy.quickstart(Root(), '/', config=conf)
Reply all
Reply to author
Forward
0 new messages