[ Pobierz całość w formacie PDF ]

for filename in glob.glob(directory +  /* ):
absPath = os.path.abspath(filename)
if os.path.isdir(absPath):
html +=   + os.path.basename(filename) +
else:
html +=   + os.path.basename(filename)
html += """"""
return html
index.exposed = True
class Download:
def index(self, filepath):
return serve_file(filepath, "application/x-download", "attachment")
index.exposed = True
if __name__ ==  __main__ :
root = Root()
root.download = Download()
cherrypy.quickstart(root)
Note that CherryPy is not the fastest for doing such things. If you think you ll have many and big downloads, put CP
BehindApache and let Apache serve those files.
Serving the favorite icon
By default, CherryPy 3 adds a  favicon_ico handler method to any root object which is mounted at  / . This is a
staticfile handler, which grabs the favicon.ico shipped in the CherryPy distribution.
To configure CherryPy to look in another file location, you can, in your server configuration, do the following:
[/favicon.ico]
tools.staticfile.on = True
tools.staticfile.filename = "/path/to/favicon.ico"
If you want a favicon, but don t want CherryPy to serve it, you can point to an HTTP URL via a link element in
the HTML head. See http://www.w3.org/2005/10/howto-favicon and http://en.wikipedia.org/wiki/Favicon for instruc-
tions.
Serving Static Content
Static content is now handled bytools.staticfileandtools.staticdirthat can easily be enabled and
configured in your config file. For instance, if you wanted to serve/style.cssfrom/home/site/style.css
and/static/*from/home/site/static/*, you can use the following configuration:
[/]
tools.staticdir.root = "/home/site"
[/style.css]
tools.staticfile.on = True
tools.staticfile.filename = "/home/site/style.css"
5.1. Features 41
CherryPy Documentation, Release 3.2.4
[/static]
tools.staticdir.on = True
tools.staticdir.dir = "static"
Parameters
" on: True or False (default). Enable or disable the filter.
" match: aregular expressionof files to match.
" filename: path to the target file.
" dir: path to the target directory.
" root: absolute path to a  root ; joined with .dir or .filename if they are relative paths.
Usage
Serving files through thestaticfiletool Directory structure
cpapp \
__init__.py
data \
scripts \
dummy.js
css \
style.css
Here is our cpapp/__init__.py:
#!python
import os.path
current_dir = os.path.dirname(os.path.abspath(__file__))
import cherrypy
class Root:
@cherrypy.expose
def index(self):
return """
CherryPy static example
Static example
"""
...and a prod.conf configuration file:
[global]
environment:  production
log.error_file:  site.log
log.screen: True
42 Chapter 5. Programmer s Guide
CherryPy Documentation, Release 3.2.4
tree.cpapp: cherrypy.Application(cpapp.Root())
[/css/style.css]
tools.staticfile.on: True
tools.staticfile.filename: cpapp.current_dir +  /data/css/style.css
[/js/some.js]
tools.staticfile.on: True
tools.staticfile.filename: cpapp.current_dir +  /data/scripts/dummy.js
Note how we use the absolute path to point at the static files. Note also that when using thestaticfiletool, the
logical URI path and the physical file do not need to be the same. Parts of their components can differ as in the case
of the Javascript resource.
You can run the above with:
$ cherryd -i cpapp -c prod.conf
Serving files through thestaticdirtool Keeping the same directory structure as above, we could have written
our config file as follows:
[/]
tools.staticdir.root: cpapp.current_dir +  data
[/css]
tools.staticdir.on: True
tools.staticdir.dir:  css
[/js]
tools.staticdir.on: True
tools.staticdir.dir:  scripts
However in this case theGET /js/some.jsrequest will fail with a404 Not Foundresponse because when
using thestaticdirtool the last segment of the URI must match exactly the path of the physical file underneath
the directory defined bytools.staticdir.dir.
In our example we must either rename the physical file or change the HTML code accordingly.
staticdir.index If tools.staticdir.index is provided, it should be the (relative) name of a file to serve for directory
requests. For example, if the staticdir.dir argument is  /home/me , the Request-URI is  myapp , and the .index arg is
 index.html , the file  /home/me/myapp/index.html will be served.
Specify the content-type of static resource Both thestaticfileandstaticdirtool allow you to spec-
ify the mime type of resources by their extension. Although internally CherryPy will most of the time guess the
correct mime type (using the Pythonmimetypesmodule), there may be cases when you need to provide the
content type values. You can do this via configuration argumentstools.staticdir.content_typesand
tools.staticfile.content_types, as in the following example.
#!python
import os.path
import cherrypy
class Root:
@cherrypy.expose
def index(self):
return """
5.1. Features 43
CherryPy Documentation, Release 3.2.4
CherryPy static tutorial
RSS 2.0
Atom 1.0
"""
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({ environment :  production ,
 log.error_file :  site.log ,
 log.screen : True})
conf = { /feed : { tools.staticdir.on : True,
 tools.staticdir.dir : os.path.join(current_dir,  feeds ),
 tools.staticdir.content_types : { rss :  application/xml ,
 atom :  application/atom+xml }}}
cherrypy.quickstart(Root(),  / , config=conf)
The value oftools.staticdir.content_typesandtools.staticfile.content_typesis a dic-
tionary whose keys are filename extensions, and values are the corresponding media-type strings (for the
Content-Typeheader). Note that the key must NOT include any leading  . .
Serve static content from a page handler bypassing the static tools It may happen that you would need the
static tools power but from a page handler itself so that you can add more processing. You can do so by calling the
serve_filefunction.
#!python
import os.path
import cherrypy
from cherrypy.lib.static import serve_file
class Root:
@cherrypy.expose
def feed(self, name):
accepts = cherrypy.request.headers.elements( Accept )
for accept in accepts:
if accept.value ==  application/atom+xml :
return serve_file(os.path.join(current_dir,  feeds ,  %s.atom % name),
content_type= application/atom+xml )
return serve_file(os.path.join(current_dir,  feeds ,  %s.rss % name),
content_type= application/xml )
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({ environment :  production ,
 log.error_file :  site.log ,
 log.screen : True})
cherrypy.quickstart(Root(),  / )
44 Chapter 5. Programmer s Guide
CherryPy Documentation, Release 3.2.4
In this example we rely on the Accept header of the HTTP request to tell us which content type is supported by the
client. If it can process the Atom content type then we serve the Atom resource, otherwise we serve the RSS one.
In any case by using the serve_file function we benefit from the CherryPy internal processing of the request in regards
of HTTP headers such as If-Modified-Since. In fact the static tools use the serve_file function.
Troubleshooting staticdir [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • kudrzwi.xlx.pl