Werkzeug Documentation Release 0.11.11-Dev November 30, 2016
Total Page:16
File Type:pdf, Size:1020Kb
Werkzeug Documentation Release 0.11.11-dev November 30, 2016 Contents I Getting Started1 1 Installation3 1.1 Installing a released version..........................3 1.2 Installing the development version.....................4 1.3 virtualenv....................................4 2 Transition to Werkzeug 1.07 2.1 Automatically Rewriting Imports......................7 2.2 Stop Using Deprecated Things........................8 3 Werkzeug Tutorial9 3.1 Introducing Shortly..............................9 3.2 Step 0: A Basic WSGI Introduction...................... 10 3.3 Step 1: Creating the Folders.......................... 11 3.4 Step 2: The Base Structure........................... 11 3.5 Intermezzo: Running the Application.................... 13 3.6 Step 3: The Environment........................... 13 3.7 Step 4: The Routing.............................. 13 3.8 Step 5: The First View............................. 14 3.9 Step 6: Redirect View............................. 16 3.10 Step 7: Detail View............................... 16 3.11 Step 8: Templates................................ 16 3.12 Step 9: The Style................................ 17 3.13 Bonus: Refinements.............................. 18 4 API Levels 19 4.1 Example..................................... 19 4.2 High or Low?.................................. 20 5 Quickstart 21 5.1 WSGI Environment.............................. 21 i 5.2 Enter Request.................................. 22 5.3 Header Parsing................................. 23 5.4 Responses.................................... 25 6 Python 3 Notes 29 6.1 WSGI Environment.............................. 29 6.2 URLs....................................... 30 6.3 Request Cleanup................................ 30 II Serving and Testing 31 7 Serving WSGI Applications 33 7.1 Reloader..................................... 35 7.2 Virtual Hosts.................................. 36 7.3 Shutting Down The Server.......................... 36 7.4 Troubleshooting................................ 37 7.5 SSL........................................ 37 8 Test Utilities 39 8.1 Diving In.................................... 39 8.2 Environment Building............................. 40 8.3 Testing API................................... 41 9 Debugging Applications 47 9.1 Enabling the Debugger............................ 47 9.2 Using the Debugger.............................. 48 9.3 Debugger PIN................................. 49 9.4 Pasting Errors.................................. 50 III Reference 51 10 Request / Response Objects 53 10.1 How they Work................................. 53 10.2 Mutability and Reusability of Wrappers................... 54 10.3 Base Wrappers................................. 54 10.4 Mixin Classes.................................. 67 11 URL Routing 75 11.1 Quickstart.................................... 75 11.2 Rule Format................................... 76 11.3 Builtin Converters............................... 76 11.4 Maps, Rules and Adapters.......................... 78 11.5 Rule Factories.................................. 87 11.6 Rule Templates................................. 88 11.7 Custom Converters............................... 88 11.8 Host Matching................................. 89 ii 12 WSGI Helpers 91 12.1 Iterator / Stream Helpers........................... 91 12.2 Environ Helpers................................ 94 12.3 Convenience Helpers............................. 99 13 Filesystem Utilities 101 14 HTTP Utilities 103 14.1 Date Functions................................. 103 14.2 Header Parsing................................. 104 14.3 Header Utilities................................. 107 14.4 Cookies..................................... 109 14.5 Conditional Response Helpers........................ 110 14.6 Constants.................................... 111 14.7 Form Data Parsing............................... 111 15 Data Structures 115 15.1 General Purpose................................ 115 15.2 HTTP Related.................................. 122 15.3 Others...................................... 133 16 Utilities 135 16.1 HTML Helpers................................. 135 16.2 General Helpers................................ 136 16.3 URL Helpers.................................. 141 16.4 UserAgent Parsing............................... 141 16.5 Security Helpers................................ 142 17 URL Helpers 145 18 Context Locals 155 19 Middlewares 161 20 HTTP Exceptions 163 20.1 Usage Example................................. 163 20.2 Error Classes.................................. 164 20.3 Baseclass..................................... 167 20.4 Special HTTP Exceptions........................... 168 20.5 Simple Aborting................................ 168 20.6 Custom Errors................................. 169 IV Deployment 171 21 Application Deployment 173 21.1 CGI........................................ 173 21.2 mod_wsgi (Apache)............................... 174 21.3 FastCGI..................................... 175 21.4 HTTP Proxying................................. 178 iii V Contributed Modules 179 22 Contributed Modules 181 22.1 Atom Syndication............................... 181 22.2 Sessions..................................... 184 22.3 Secure Cookie.................................. 187 22.4 Cache...................................... 191 22.5 Extra Wrappers................................. 197 22.6 Iter IO...................................... 199 22.7 Fixers...................................... 201 22.8 WSGI Application Profiler.......................... 203 22.9 Lint Validation Middleware.......................... 204 VI Additional Information 205 23 Important Terms 207 23.1 WSGI....................................... 207 23.2 Response Object................................ 207 23.3 View Function................................. 207 24 Unicode 209 24.1 Unicode in Python............................... 209 24.2 Unicode in HTTP................................ 210 24.3 Error Handling................................. 210 24.4 Request and Response Objects........................ 211 24.5 The Filesystem................................. 211 25 Dealing with Request Data 213 25.1 Missing EOF Marker on Input Stream.................... 213 25.2 When does Werkzeug Parse?......................... 213 25.3 How does it Parse?............................... 214 25.4 Limiting Request Data............................. 214 25.5 How to extend Parsing?............................ 214 26 Werkzeug Changelog 217 26.1 Werkzeug Changelog............................. 217 26.2 API Changes.................................. 242 iv Part I GETTING STARTED If you are new to Werkzeug or WSGI development in general you should start here. 1 2 CHAPTER 1 Installation Werkzeug requires at least Python 2.6 to work correctly. If you do need to support an older version you can download an older version of Werkzeug though we strongly recommend against that. Werkzeug currently has experimental support for Python 3. For more information about the Python 3 support see Python 3 Notes. 1.1 Installing a released version 1.1.1 As a Python egg (via easy_install or pip) You can install the most recent Werkzeug version using easy_install: easy_install Werkzeug Alternatively you can also use pip: pip install Werkzeug Either way we strongly recommend using these tools in combination with virtualenv. This will install a Werkzeug egg in your Python installation’s site-packages directory. 1.1.2 From the tarball release 1. Download the most recent tarball from the download page. 2. Unpack the tarball. 3. python setup.py install Note that the last command will automatically download and install setuptools if you don’t already have it installed. This requires a working Internet connection. This will install Werkzeug into your Python installation’s site-packages directory. 3 1.2 Installing the development version 1. Install Git 2. git clone git://github.com/mitsuhiko/werkzeug.git 3. cd werkzeug 4. pip install --editable . 1.3 virtualenv Virtualenv is probably what you want to use during development, and in production too if you have shell access there. What problem does virtualenv solve? If you like Python as I do, chances are you want to use it for other projects besides Werkzeug-based web applications. But the more projects you have, the more likely it is that you will be working with different versions of Python itself, or at least different versions of Python libraries. Let’s face it; quite often libraries break backwards compatibility, and it’s unlikely that any serious application will have zero dependencies. So what do you do if two or more of your projects have conflicting dependencies? Virtualenv to the rescue! It basically enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated. So let’s see how virtualenv works! If you are on Mac OS X or Linux, chances are that one of the following two commands will work for you: $ sudo easy_install virtualenv or even better: $ sudo pip install virtualenv One of these will probably install virtualenv on your system. Maybe it’s even in your package manager. If you use Ubuntu, try: $ sudo apt-get install python-virtualenv If you are on Windows and don’t have the easy_install command, you must install it first. Once you have it installed, run the same commands as above, but without the sudo prefix. Once you have virtualenv installed, just fire up a