Changeset 727
- Timestamp:
- 10/08/05 13:44:09
- Files:
-
- trunk/cherrypy/_cputil.py (modified) (1 diff)
- trunk/cherrypy/test/test_tutorials.py (modified) (1 diff)
- trunk/cherrypy/tutorial/custom_error.html (modified) (1 diff)
- trunk/cherrypy/tutorial/tut01_helloworld.py (modified) (1 diff)
- trunk/cherrypy/tutorial/tut02_expose_methods.py (modified) (1 diff)
- trunk/cherrypy/tutorial/tut03_get_and_post.py (modified) (1 diff)
- trunk/cherrypy/tutorial/tut04_complex_site.py (modified) (1 diff)
- trunk/cherrypy/tutorial/tut05_derived_objects.py (modified) (1 diff)
- trunk/cherrypy/tutorial/tut06_default_method.py (modified) (1 diff)
- trunk/cherrypy/tutorial/tut07_sessions.py (modified) (1 diff)
- trunk/cherrypy/tutorial/tut09_files.py (modified) (1 diff)
- trunk/cherrypy/tutorial/tut10_http_errors.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cputil.py
r709 r727 211 211 if errorPageFile: 212 212 try: 213 template = file(errorPageFile, 'rb') 213 template = file(errorPageFile, 'rb').read() 214 214 except: 215 215 m = kwargs['message'] trunk/cherrypy/test/test_tutorials.py
r723 r727 172 172 self.assertHeader("Content-Disposition", "attachment; filename=pdf_file.pdf") 173 173 self.assertEqual(len(self.body), 85698) 174 175 def test10HTTPErrors(self): 176 self.load_tut_module("tut10_http_errors") 177 178 self.getPage("/") 179 self.assertInBody("""<a href="toggleTracebacks">""") 180 self.assertInBody("""<a href="/doesNotExist">""") 181 self.assertInBody("""<a href="/error?code=403">""") 182 self.assertInBody("""<a href="/error?code=500">""") 183 self.assertInBody("""<a href="/messageArg">""") 184 185 tracebacks = cherrypy.config.get('server.showTracebacks') 186 self.getPage("/toggleTracebacks") 187 self.assertEqual(cherrypy.config.get('server.showTracebacks'), not tracebacks) 188 self.assertStatus("302 Found") 189 190 self.getPage("/error?code=500") 191 self.assertStatus("500 Internal error") 192 self.assertInBody("Server got itself in trouble") 193 194 self.getPage("/error?code=403") 195 self.assertStatus("403 Forbidden") 196 self.assertInBody("<h2>You can't do that!</h2>") 197 198 self.getPage("/messageArg") 199 self.assertStatus("500 Internal error") 200 self.assertInBody("If you construct an HTTPError with a 'message'") 201 174 202 175 203 if __name__ == "__main__": trunk/cherrypy/tutorial/custom_error.html
r650 r727 7 7 </head> 8 8 <body> 9 10 9 <h2>You can't do that!</h2> 10 <p>%(message)s</p> 11 11 <p>This is a custom error page that is read from a file.<p> 12 <p>%(traceback)s</p> 12 13 </body> 13 14 14 </html> 15 16 17 <?xml version="1.0" encoding="UTF-8"?>18 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"19 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">20 <html>21 <head>22 <title>403 Unauthorized</title>23 </head>24 <body>25 26 <h2>You can't do that!</h2>27 <p>This is a custom error page that is read from a file.<p>28 </body>29 30 </html>31 32 trunk/cherrypy/tutorial/tut01_helloworld.py
r382 r727 1 1 """ 2 Tutorial 01- Hello World2 Tutorial - Hello World 3 3 4 4 The most basic (working) CherryPy application possible. trunk/cherrypy/tutorial/tut02_expose_methods.py
r382 r727 1 1 """ 2 Tutorial 02- Multiple methods2 Tutorial - Multiple methods 3 3 4 4 This tutorial shows you how to link to other methods of your request trunk/cherrypy/tutorial/tut03_get_and_post.py
r382 r727 1 1 """ 2 Tutorial 03- Passing variables2 Tutorial - Passing variables 3 3 4 4 This tutorial shows you how to pass GET/POST variables to methods. trunk/cherrypy/tutorial/tut04_complex_site.py
r382 r727 1 1 """ 2 Tutorial 04- Multiple objects2 Tutorial - Multiple objects 3 3 4 4 This tutorial shows you how to create a site structure through multiple trunk/cherrypy/tutorial/tut05_derived_objects.py
r382 r727 1 1 """ 2 Tutorial 05- Object inheritance2 Tutorial - Object inheritance 3 3 4 4 You are free to derive your request handler classes from any base trunk/cherrypy/tutorial/tut06_default_method.py
r382 r727 1 1 """ 2 Tutorial 07- The default method2 Tutorial - The default method 3 3 4 4 Request handler objects can implement a method called "default" that trunk/cherrypy/tutorial/tut07_sessions.py
r522 r727 1 1 """ 2 Tutorial 08- Sessions2 Tutorial - Sessions 3 3 4 4 Storing session data in CherryPy applications is very easy: cherrypy.request trunk/cherrypy/tutorial/tut09_files.py
r723 r727 1 """Tutorial: File upload""" 1 """ 2 3 Tutorial: File upload and download 4 5 Uploads 6 ------- 7 8 When a client uploads a file to a CherryPy application, it's placed 9 on disk immediately. CherryPy will pass it to your exposed method 10 as an argument (see "myFile" below); that arg will have a "file" 11 attribute, which is a handle to the temporary uploaded file. 12 If you wish to permanently save the file, you need to read() 13 from myFile.file and write() somewhere else. 14 15 Note the use of 'enctype="multipart/form-data"' and 'input type="file"' 16 in the HTML which the client uses to upload the file. 17 18 19 Downloads 20 --------- 21 22 If you wish to send a file to the client, you have two options: 23 First, you can simply return a file-like object from your page handler. 24 CherryPy will read the file and serve it as the content (HTTP body) 25 of the response. However, that doesn't tell the client that 26 the response is a file to be saved, rather than displayed. 27 Use cherrypy.lib.cptools.serveFile for that; it takes four 28 arguments: 29 30 serveFile(path, contentType=None, disposition=None, name=None) 31 32 Set "name" to the filename that you expect clients to use when they save 33 your file. Note that the "name" argument is ignored if you don't also 34 provide a "disposition" ("application/x-download" works in most cases). 35 36 """ 2 37 3 38 import os trunk/cherrypy/tutorial/tut10_http_errors.py
r650 r727 1 """Tutorial: http errors """ 1 """ 2 3 Tutorial: HTTP errors 4 5 HTTPError is used to return an error response to the client. 6 CherryPy has lots of options regarding how such errors are 7 logged, displayed, and formatted. 8 9 """ 2 10 3 11 import cherrypy 4 12 5 # we want to customize 403 errors6 customErrors = {7 'errorPage.403' : "custom_error.html"8 }9 10 cherrypy.config.update({'/' : customErrors})11 13 12 14 class HTTPErrorDemo(object): … … 22 24 return """ 23 25 <html><body> 24 <h2><a href="toggleTracebacks">Toggle tracebacks %s</a>< br/><br/></h2>25 < a href="/doesNotExist">Click me i'm a broken link!</a>26 < br/><br/>27 < a href="/error?code=403">Use a custom an error page from a file.</a>28 < br/><br/>29 These errors are explicitly raised by the application.30 <a href="/error?code=400">400</a>31 <a href="/error?code=401">401</a>32 <a href="/error?code=402">402</a>33 < a href="/error?code=500">500</a>34 < br/><br/>35 <a href="/bodyArg">You can also set the response body when you raise an error</a>26 <h2><a href="toggleTracebacks">Toggle tracebacks %s</a></h2> 27 <p><a href="/doesNotExist">Click me; I'm a broken link!</a></p> 28 <p><a href="/error?code=403">Use a custom an error page from a file.</a></p> 29 <p>These errors are explicitly raised by the application:</p> 30 <ul> 31 <li><a href="/error?code=400">400</a></li> 32 <li><a href="/error?code=401">401</a></li> 33 <li><a href="/error?code=402">402</a></li> 34 <li><a href="/error?code=500">500</a></li> 35 </ul> 36 <p><a href="/messageArg">You can also set the response body 37 when you raise an error.</a></p> 36 38 </body></html> 37 39 """ % trace 38 40 index.exposed = True 39 41 40 42 def toggleTracebacks(self): 41 43 # simple function to toggle tracebacks on and off … … 44 46 45 47 # redirect back to the index 46 raise cherrypy. _cperror.HTTPRedirect('/')47 toggleTracebacks.exposed =True48 raise cherrypy.HTTPRedirect('/') 49 toggleTracebacks.exposed = True 48 50 49 51 def error(self, code): 50 52 # raise an error based on the get query 51 code = int(code)52 53 raise cherrypy.HTTPError(status = code) 53 54 error.exposed = True 55 56 def messageArg(self): 57 message = ("If you construct an HTTPError with a 'message' " 58 "argument, it wil be placed on the error page " 59 "(underneath the status line by default).") 60 raise cherrypy.HTTPError(500, message=message) 61 messageArg.exposed = True 54 62 55 def bodyArg(self):56 message = """ If you construct a HTTPError wiht body argument, the body argument57 will overide any default or custom error page.58 """59 raise cherrypy.HTTPError(403, body = message)60 bodyArg.exposed = True61 63 62 64 cherrypy.root = HTTPErrorDemo() 65 66 # Set a custom response for 403 errors. 67 import os 68 localDir = os.path.dirname(__file__) 69 curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) 70 cherrypy.config.update({'errorPage.403' : os.path.join(curpath, "custom_error.html")}) 71 63 72 64 73 if __name__ == '__main__':

