diff options
| author | Robert Brewer <fumanchu@aminus.org> | 2005-10-08 18:44:09 +0000 |
|---|---|---|
| committer | Robert Brewer <fumanchu@aminus.org> | 2005-10-08 18:44:09 +0000 |
| commit | 5e79e4fa67cb3b24f137e3cd908b8c924c562d1e (patch) | |
| tree | 32817a0bd8fd2411b98253d7300994701f368374 /cherrypy/tutorial | |
| parent | b7d24914e656586be38d25a47095ebc2ade561c1 (diff) | |
| download | cherrypy-git-5e79e4fa67cb3b24f137e3cd908b8c924c562d1e.tar.gz | |
Tutorial fixes, plus a bug in _cputil.getErrorPage.
Diffstat (limited to 'cherrypy/tutorial')
| -rw-r--r-- | cherrypy/tutorial/custom_error.html | 28 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut01_helloworld.py | 2 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut02_expose_methods.py | 2 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut03_get_and_post.py | 2 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut04_complex_site.py | 2 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut05_derived_objects.py | 2 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut06_default_method.py | 2 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut07_sessions.py | 2 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut09_files.py | 37 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut10_http_errors.py | 67 |
10 files changed, 86 insertions, 60 deletions
diff --git a/cherrypy/tutorial/custom_error.html b/cherrypy/tutorial/custom_error.html index 7bd9934f..22ac603b 100644 --- a/cherrypy/tutorial/custom_error.html +++ b/cherrypy/tutorial/custom_error.html @@ -6,27 +6,9 @@ <title>403 Unauthorized</title> </head> <body> - - <h2>You can't do that!</h2> - <p>This is a custom error page that is read from a file.<p> + <h2>You can't do that!</h2>
+ <p>%(message)s</p> + <p>This is a custom error page that is read from a file.<p>
+ <p>%(traceback)s</p> </body> - -</html> - - -<?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> -<html> -<head> - <title>403 Unauthorized</title> -</head> - <body> - - <h2>You can't do that!</h2> - <p>This is a custom error page that is read from a file.<p> - </body> - -</html> - - +</html>
\ No newline at end of file diff --git a/cherrypy/tutorial/tut01_helloworld.py b/cherrypy/tutorial/tut01_helloworld.py index 75dd4039..7694f470 100644 --- a/cherrypy/tutorial/tut01_helloworld.py +++ b/cherrypy/tutorial/tut01_helloworld.py @@ -1,5 +1,5 @@ """ -Tutorial 01 - Hello World +Tutorial - Hello World The most basic (working) CherryPy application possible. """ diff --git a/cherrypy/tutorial/tut02_expose_methods.py b/cherrypy/tutorial/tut02_expose_methods.py index 777dd11f..25a763eb 100644 --- a/cherrypy/tutorial/tut02_expose_methods.py +++ b/cherrypy/tutorial/tut02_expose_methods.py @@ -1,5 +1,5 @@ """ -Tutorial 02 - Multiple methods +Tutorial - Multiple methods This tutorial shows you how to link to other methods of your request handler. diff --git a/cherrypy/tutorial/tut03_get_and_post.py b/cherrypy/tutorial/tut03_get_and_post.py index ca9bd184..72d9b196 100644 --- a/cherrypy/tutorial/tut03_get_and_post.py +++ b/cherrypy/tutorial/tut03_get_and_post.py @@ -1,5 +1,5 @@ """ -Tutorial 03 - Passing variables +Tutorial - Passing variables This tutorial shows you how to pass GET/POST variables to methods. """ diff --git a/cherrypy/tutorial/tut04_complex_site.py b/cherrypy/tutorial/tut04_complex_site.py index 1ae7a57d..9df48d9c 100644 --- a/cherrypy/tutorial/tut04_complex_site.py +++ b/cherrypy/tutorial/tut04_complex_site.py @@ -1,5 +1,5 @@ """ -Tutorial 04 - Multiple objects +Tutorial - Multiple objects This tutorial shows you how to create a site structure through multiple possibly nested request handler objects. diff --git a/cherrypy/tutorial/tut05_derived_objects.py b/cherrypy/tutorial/tut05_derived_objects.py index 786a47c4..8803bc06 100644 --- a/cherrypy/tutorial/tut05_derived_objects.py +++ b/cherrypy/tutorial/tut05_derived_objects.py @@ -1,5 +1,5 @@ """ -Tutorial 05 - Object inheritance +Tutorial - Object inheritance You are free to derive your request handler classes from any base class you wish. In most real-world applications, you will probably diff --git a/cherrypy/tutorial/tut06_default_method.py b/cherrypy/tutorial/tut06_default_method.py index e1349575..f43747d8 100644 --- a/cherrypy/tutorial/tut06_default_method.py +++ b/cherrypy/tutorial/tut06_default_method.py @@ -1,5 +1,5 @@ """ -Tutorial 07 - The default method +Tutorial - The default method Request handler objects can implement a method called "default" that is called when no other suitable method/object could be found. diff --git a/cherrypy/tutorial/tut07_sessions.py b/cherrypy/tutorial/tut07_sessions.py index 1c8c904e..609e2656 100644 --- a/cherrypy/tutorial/tut07_sessions.py +++ b/cherrypy/tutorial/tut07_sessions.py @@ -1,5 +1,5 @@ """ -Tutorial 08 - Sessions +Tutorial - Sessions Storing session data in CherryPy applications is very easy: cherrypy.request provides a dictionary called sessionMap that represents the session diff --git a/cherrypy/tutorial/tut09_files.py b/cherrypy/tutorial/tut09_files.py index 35b2ea53..87d294ad 100644 --- a/cherrypy/tutorial/tut09_files.py +++ b/cherrypy/tutorial/tut09_files.py @@ -1,4 +1,39 @@ -"""Tutorial: File upload""" +""" + +Tutorial: File upload and download + +Uploads +------- + +When a client uploads a file to a CherryPy application, it's placed +on disk immediately. CherryPy will pass it to your exposed method +as an argument (see "myFile" below); that arg will have a "file" +attribute, which is a handle to the temporary uploaded file. +If you wish to permanently save the file, you need to read() +from myFile.file and write() somewhere else. + +Note the use of 'enctype="multipart/form-data"' and 'input type="file"' +in the HTML which the client uses to upload the file. + + +Downloads +--------- + +If you wish to send a file to the client, you have two options: +First, you can simply return a file-like object from your page handler. +CherryPy will read the file and serve it as the content (HTTP body) +of the response. However, that doesn't tell the client that +the response is a file to be saved, rather than displayed. +Use cherrypy.lib.cptools.serveFile for that; it takes four +arguments: + +serveFile(path, contentType=None, disposition=None, name=None) + +Set "name" to the filename that you expect clients to use when they save +your file. Note that the "name" argument is ignored if you don't also +provide a "disposition" ("application/x-download" works in most cases). + +""" import os localDir = os.path.dirname(__file__) diff --git a/cherrypy/tutorial/tut10_http_errors.py b/cherrypy/tutorial/tut10_http_errors.py index 4f8ece9d..df276438 100644 --- a/cherrypy/tutorial/tut10_http_errors.py +++ b/cherrypy/tutorial/tut10_http_errors.py @@ -1,13 +1,15 @@ -"""Tutorial: http errors """ +""" -import cherrypy +Tutorial: HTTP errors + +HTTPError is used to return an error response to the client. +CherryPy has lots of options regarding how such errors are +logged, displayed, and formatted. -# we want to customize 403 errors -customErrors = { - 'errorPage.403' : "custom_error.html" - } +""" + +import cherrypy -cherrypy.config.update({'/' : customErrors}) class HTTPErrorDemo(object): @@ -21,46 +23,53 @@ class HTTPErrorDemo(object): return """ <html><body> - <h2><a href="toggleTracebacks">Toggle tracebacks %s</a><br/><br/></h2> - <a href="/doesNotExist">Click me i'm a broken link!</a> - <br/><br/> - <a href="/error?code=403">Use a custom an error page from a file.</a> - <br/><br/> - These errors are explicitly raised by the application. - <a href="/error?code=400">400</a> - <a href="/error?code=401">401</a> - <a href="/error?code=402">402</a> - <a href="/error?code=500">500</a> - <br/><br/> - <a href="/bodyArg">You can also set the response body when you raise an error</a> + <h2><a href="toggleTracebacks">Toggle tracebacks %s</a></h2> + <p><a href="/doesNotExist">Click me; I'm a broken link!</a></p> + <p><a href="/error?code=403">Use a custom an error page from a file.</a></p> + <p>These errors are explicitly raised by the application:</p> + <ul> + <li><a href="/error?code=400">400</a></li> + <li><a href="/error?code=401">401</a></li> + <li><a href="/error?code=402">402</a></li> + <li><a href="/error?code=500">500</a></li> + </ul> + <p><a href="/messageArg">You can also set the response body + when you raise an error.</a></p> </body></html> """ % trace index.exposed = True - + def toggleTracebacks(self): # simple function to toggle tracebacks on and off tracebacks = cherrypy.config.get('server.showTracebacks') cherrypy.config.update({'server.showTracebacks': not tracebacks}) # redirect back to the index - raise cherrypy._cperror.HTTPRedirect('/') - toggleTracebacks.exposed=True + raise cherrypy.HTTPRedirect('/') + toggleTracebacks.exposed = True def error(self, code): # raise an error based on the get query - code = int(code) raise cherrypy.HTTPError(status = code) error.exposed = True + + def messageArg(self): + message = ("If you construct an HTTPError with a 'message' " + "argument, it wil be placed on the error page " + "(underneath the status line by default).") + raise cherrypy.HTTPError(500, message=message) + messageArg.exposed = True - def bodyArg(self): - message = """ If you construct a HTTPError wiht body argument, the body argument - will overide any default or custom error page. - """ - raise cherrypy.HTTPError(403, body = message) - bodyArg.exposed = True cherrypy.root = HTTPErrorDemo() +# Set a custom response for 403 errors. +import os +localDir = os.path.dirname(__file__) +curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) +cherrypy.config.update({'errorPage.403' : os.path.join(curpath, "custom_error.html")}) + + if __name__ == '__main__': # Use the configuration file tutorial.conf. cherrypy.config.update(file = 'tutorial.conf') |
