This file is indexed.

/usr/share/doc/python-bottle-doc/html/recipes.html is in python-bottle-doc 0.12.13-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Recipes &#8212; Bottle 0.12.13 documentation</title>
    
    <link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '0.12.13',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Bottle 0.12.13 documentation" href="index.html" />
    <link rel="next" title="Frequently Asked Questions" href="faq.html" />
    <link rel="prev" title="Primer to Asynchronous Applications" href="async.html" />
   
  <link rel="stylesheet" href="_static/custom.css" type="text/css" />
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head>
  <body role="document">
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <span class="target" id="module-bottle"></span><div class="section" id="recipes">
<h1>Recipes<a class="headerlink" href="#recipes" title="Permalink to this headline"></a></h1>
<p>This is a collection of code snippets and examples for common use cases.</p>
<div class="section" id="keeping-track-of-sessions">
<h2>Keeping track of Sessions<a class="headerlink" href="#keeping-track-of-sessions" title="Permalink to this headline"></a></h2>
<p>There is no built-in support for sessions because there is no <em>right</em> way to do it (in a micro framework). Depending on requirements and environment you could use <a class="reference external" href="http://beaker.groovie.org/">beaker</a> middleware with a fitting backend or implement it yourself. Here is an example for beaker sessions with a file-based backend:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">bottle</span>
<span class="kn">from</span> <span class="nn">beaker.middleware</span> <span class="k">import</span> <span class="n">SessionMiddleware</span>

<span class="n">session_opts</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;session.type&#39;</span><span class="p">:</span> <span class="s1">&#39;file&#39;</span><span class="p">,</span>
    <span class="s1">&#39;session.cookie_expires&#39;</span><span class="p">:</span> <span class="mi">300</span><span class="p">,</span>
    <span class="s1">&#39;session.data_dir&#39;</span><span class="p">:</span> <span class="s1">&#39;./data&#39;</span><span class="p">,</span>
    <span class="s1">&#39;session.auto&#39;</span><span class="p">:</span> <span class="kc">True</span>
<span class="p">}</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">SessionMiddleware</span><span class="p">(</span><span class="n">bottle</span><span class="o">.</span><span class="n">app</span><span class="p">(),</span> <span class="n">session_opts</span><span class="p">)</span>

<span class="nd">@bottle</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s1">&#39;/test&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
  <span class="n">s</span> <span class="o">=</span> <span class="n">bottle</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;beaker.session&#39;</span><span class="p">)</span>
  <span class="n">s</span><span class="p">[</span><span class="s1">&#39;test&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;test&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
  <span class="n">s</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
  <span class="k">return</span> <span class="s1">&#39;Test counter: </span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">s</span><span class="p">[</span><span class="s1">&#39;test&#39;</span><span class="p">]</span>

<span class="n">bottle</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">app</span><span class="o">=</span><span class="n">app</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="debugging-with-style-debugging-middleware">
<h2>Debugging with Style: Debugging Middleware<a class="headerlink" href="#debugging-with-style-debugging-middleware" title="Permalink to this headline"></a></h2>
<p>Bottle catches all Exceptions raised in your app code to prevent your WSGI server from crashing. If the built-in <a class="reference internal" href="api.html#bottle.debug" title="bottle.debug"><code class="xref py py-func docutils literal"><span class="pre">debug()</span></code></a> mode is not enough and you need exceptions to propagate to a debugging middleware, you can turn off this behaviour:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">bottle</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">bottle</span><span class="o">.</span><span class="n">app</span><span class="p">()</span>
<span class="n">app</span><span class="o">.</span><span class="n">catchall</span> <span class="o">=</span> <span class="kc">False</span> <span class="c1">#Now most exceptions are re-raised within bottle.</span>
<span class="n">myapp</span> <span class="o">=</span> <span class="n">DebuggingMiddleware</span><span class="p">(</span><span class="n">app</span><span class="p">)</span> <span class="c1">#Replace this with a middleware of your choice (see below)</span>
<span class="n">bottle</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">app</span><span class="o">=</span><span class="n">myapp</span><span class="p">)</span>
</pre></div>
</div>
<p>Now, bottle only catches its own exceptions (<a class="reference internal" href="api.html#bottle.HTTPError" title="bottle.HTTPError"><code class="xref py py-exc docutils literal"><span class="pre">HTTPError</span></code></a>, <a class="reference internal" href="api.html#bottle.HTTPResponse" title="bottle.HTTPResponse"><code class="xref py py-exc docutils literal"><span class="pre">HTTPResponse</span></code></a> and <a class="reference internal" href="api.html#bottle.BottleException" title="bottle.BottleException"><code class="xref py py-exc docutils literal"><span class="pre">BottleException</span></code></a>) and your middleware can handle the rest.</p>
<p>The <a class="reference external" href="http://werkzeug.pocoo.org/documentation/dev/debug.html">werkzeug</a> and <a class="reference external" href="http://pythonpaste.org/modules/evalexception.html">paste</a> libraries both ship with very powerful debugging WSGI middleware. Look at <code class="xref py py-class docutils literal"><span class="pre">werkzeug.debug.DebuggedApplication</span></code> for <a class="reference external" href="http://werkzeug.pocoo.org/documentation/dev/debug.html">werkzeug</a> and <code class="xref py py-class docutils literal"><span class="pre">paste.evalexception.middleware.EvalException</span></code> for <a class="reference external" href="http://pythonpaste.org/modules/evalexception.html">paste</a>. They both allow you do inspect the stack and even execute python code within the stack context, so <strong>do not use them in production</strong>.</p>
</div>
<div class="section" id="unit-testing-bottle-applications">
<h2>Unit-Testing Bottle Applications<a class="headerlink" href="#unit-testing-bottle-applications" title="Permalink to this headline"></a></h2>
<p>Unit-testing is usually performed against methods defined in your web application without running a WSGI environment.</p>
<p>A simple example using <a class="reference external" href="http://readthedocs.org/docs/nose">Nose</a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">bottle</span>

<span class="nd">@bottle</span><span class="o">.</span><span class="n">route</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">():</span>
    <span class="k">return</span> <span class="s1">&#39;Hi!&#39;</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">bottle</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
</pre></div>
</div>
<p>Test script:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mywebapp</span>

<span class="k">def</span> <span class="nf">test_webapp_index</span><span class="p">():</span>
    <span class="k">assert</span> <span class="n">mywebapp</span><span class="o">.</span><span class="n">index</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;Hi!&#39;</span>
</pre></div>
</div>
<p>In the example the Bottle route() method is never executed - only index() is tested.</p>
</div>
<div class="section" id="functional-testing-bottle-applications">
<h2>Functional Testing Bottle Applications<a class="headerlink" href="#functional-testing-bottle-applications" title="Permalink to this headline"></a></h2>
<p>Any HTTP-based testing system can be used with a running WSGI server, but some testing frameworks work more intimately with WSGI, and provide the ability the call WSGI applications in a controlled environment, with tracebacks and full use of debugging tools. <a class="reference external" href="http://www.wsgi.org/en/latest/testing.html">Testing tools for WSGI</a> is a good starting point.</p>
<p>Example using <a class="reference external" href="http://webtest.pythonpaste.org/">WebTest</a> and <a class="reference external" href="http://readthedocs.org/docs/nose">Nose</a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">webtest</span> <span class="k">import</span> <span class="n">TestApp</span>
<span class="kn">import</span> <span class="nn">mywebapp</span>

<span class="k">def</span> <span class="nf">test_functional_login_logout</span><span class="p">():</span>
    <span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="n">mywebapp</span><span class="o">.</span><span class="n">app</span><span class="p">)</span>

    <span class="n">app</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s1">&#39;/login&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;user&#39;</span><span class="p">:</span> <span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;pass&#39;</span><span class="p">:</span> <span class="s1">&#39;bar&#39;</span><span class="p">})</span> <span class="c1"># log in and get a cookie</span>

    <span class="k">assert</span> <span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;/admin&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="s1">&#39;200 OK&#39;</span>        <span class="c1"># fetch a page successfully</span>

    <span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;/logout&#39;</span><span class="p">)</span>                                 <span class="c1"># log out</span>
    <span class="n">app</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>                                        <span class="c1"># drop the cookie</span>

    <span class="c1"># fetch the same page, unsuccessfully</span>
    <span class="k">assert</span> <span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;/admin&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="s1">&#39;401 Unauthorized&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="embedding-other-wsgi-apps">
<h2>Embedding other WSGI Apps<a class="headerlink" href="#embedding-other-wsgi-apps" title="Permalink to this headline"></a></h2>
<p>This is not the recommend way (you should use a middleware in front of bottle to do this) but you can call other WSGI applications from within your bottle app and let bottle act as a pseudo-middleware. Here is an example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">bottle</span> <span class="k">import</span> <span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">route</span>
<span class="n">subproject</span> <span class="o">=</span> <span class="n">SomeWSGIApplication</span><span class="p">()</span>

<span class="nd">@route</span><span class="p">(</span><span class="s1">&#39;/subproject/:subpath#.*#&#39;</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;ANY&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">call_wsgi</span><span class="p">(</span><span class="n">subpath</span><span class="p">):</span>
    <span class="n">new_environ</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
    <span class="n">new_environ</span><span class="p">[</span><span class="s1">&#39;SCRIPT_NAME&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;SCRIPT_NAME&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;/subproject&#39;</span>
    <span class="n">new_environ</span><span class="p">[</span><span class="s1">&#39;PATH_INFO&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">subpath</span>
    <span class="k">def</span> <span class="nf">start_response</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">headerlist</span><span class="p">):</span>
        <span class="n">response</span><span class="o">.</span><span class="n">status</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">status</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">headerlist</span><span class="p">:</span>
            <span class="n">response</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">app</span><span class="p">(</span><span class="n">new_environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>
</pre></div>
</div>
<p>Again, this is not the recommend way to implement subprojects. It is only here because many people asked for this and to show how bottle maps to WSGI.</p>
</div>
<div class="section" id="ignore-trailing-slashes">
<h2>Ignore trailing slashes<a class="headerlink" href="#ignore-trailing-slashes" title="Permalink to this headline"></a></h2>
<p>For Bottle, <code class="docutils literal"><span class="pre">/example</span></code> and <code class="docutils literal"><span class="pre">/example/</span></code> are two different routes <a class="footnote-reference" href="#id3" id="id2">[1]</a>. To treat both URLs the same you can add two <code class="docutils literal"><span class="pre">&#64;route</span></code> decorators:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@route</span><span class="p">(</span><span class="s1">&#39;/test&#39;</span><span class="p">)</span>
<span class="nd">@route</span><span class="p">(</span><span class="s1">&#39;/test/&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test</span><span class="p">():</span> <span class="k">return</span> <span class="s1">&#39;Slash? no?&#39;</span>
</pre></div>
</div>
<p>or add a WSGI middleware that strips trailing slashes from all URLs:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">StripPathMiddleware</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app</span><span class="p">):</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">app</span> <span class="o">=</span> <span class="n">app</span>
  <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">h</span><span class="p">):</span>
    <span class="n">e</span><span class="p">[</span><span class="s1">&#39;PATH_INFO&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">e</span><span class="p">[</span><span class="s1">&#39;PATH_INFO&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">app</span><span class="p">(</span><span class="n">e</span><span class="p">,</span><span class="n">h</span><span class="p">)</span>

<span class="n">app</span> <span class="o">=</span> <span class="n">bottle</span><span class="o">.</span><span class="n">app</span><span class="p">()</span>
<span class="n">myapp</span> <span class="o">=</span> <span class="n">StripPathMiddleware</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="n">bottle</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">app</span><span class="o">=</span><span class="n">myapp</span><span class="p">)</span>
</pre></div>
</div>
<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id3" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[1]</a></td><td>Because they are. See &lt;<a class="reference external" href="http://www.ietf.org/rfc/rfc3986.txt">http://www.ietf.org/rfc/rfc3986.txt</a>&gt;</td></tr>
</tbody>
</table>
</div>
<div class="section" id="keep-alive-requests">
<h2>Keep-alive requests<a class="headerlink" href="#keep-alive-requests" title="Permalink to this headline"></a></h2>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For a more detailed explanation, see <a class="reference internal" href="async.html"><span class="doc">Primer to Asynchronous Applications</span></a>.</p>
</div>
<p>Several &#8220;push&#8221; mechanisms like XHR multipart need the ability to write response data without closing the connection in conjunction with the response header &#8220;Connection: keep-alive&#8221;. WSGI does not easily lend itself to this behavior, but it is still possible to do so in Bottle by using the <a class="reference external" href="http://www.gevent.org/">gevent</a> async framework. Here is a sample that works with either the <a class="reference external" href="http://www.gevent.org/">gevent</a> HTTP server or the <a class="reference external" href="http://pythonpaste.org/modules/evalexception.html">paste</a> HTTP server (it may work with others, but I have not tried). Just change <code class="docutils literal"><span class="pre">server='gevent'</span></code> to <code class="docutils literal"><span class="pre">server='paste'</span></code> to use the <a class="reference external" href="http://pythonpaste.org/modules/evalexception.html">paste</a> server:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">gevent</span> <span class="k">import</span> <span class="n">monkey</span><span class="p">;</span> <span class="n">monkey</span><span class="o">.</span><span class="n">patch_all</span><span class="p">()</span>

<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">bottle</span> <span class="k">import</span> <span class="n">route</span><span class="p">,</span> <span class="n">run</span>

<span class="nd">@route</span><span class="p">(</span><span class="s1">&#39;/stream&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">stream</span><span class="p">():</span>
    <span class="k">yield</span> <span class="s1">&#39;START&#39;</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
    <span class="k">yield</span> <span class="s1">&#39;MIDDLE&#39;</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
    <span class="k">yield</span> <span class="s1">&#39;END&#39;</span>

<span class="n">run</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="s1">&#39;0.0.0.0&#39;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">8080</span><span class="p">,</span> <span class="n">server</span><span class="o">=</span><span class="s1">&#39;gevent&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>If you browse to <code class="docutils literal"><span class="pre">http://localhost:8080/stream</span></code>, you should see &#8216;START&#8217;, &#8216;MIDDLE&#8217;, and &#8216;END&#8217; show up one at a time (rather than waiting 8 seconds to see them all at once).</p>
</div>
<div class="section" id="gzip-compression-in-bottle">
<h2>Gzip Compression in Bottle<a class="headerlink" href="#gzip-compression-in-bottle" title="Permalink to this headline"></a></h2>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For a detailed discussion, see <a class="reference external" href="https://github.com/defnull/bottle/issues/92">compression</a></p>
</div>
<p>A common feature request is for Bottle to support Gzip compression, which speeds up sites by compressing static resources (like CSS and JS files) during a request.</p>
<p>Supporting Gzip compression is not a straightforward proposition, due to a number of corner cases that crop up frequently. A proper Gzip implementation must:</p>
<ul class="simple">
<li>Compress on the fly and be fast doing so.</li>
<li>Do not compress for browsers that don&#8217;t support it.</li>
<li>Do not compress files that are compressed already (images, videos).</li>
<li>Do not compress dynamic files.</li>
<li>Support two differed compression algorithms (gzip and deflate).</li>
<li>Cache compressed files that don&#8217;t change often.</li>
<li>De-validate the cache if one of the files changed anyway.</li>
<li>Make sure the cache does not get to big.</li>
<li>Do not cache small files because a disk seek would take longer than on-the-fly compression.</li>
</ul>
<p>Because of these requirements, it is the recommendation of the Bottle project that Gzip compression is best handled by the WSGI server Bottle runs on top of. WSGI servers such as <a class="reference external" href="http://www.cherrypy.org">cherrypy</a> provide a <a class="reference external" href="http://www.cherrypy.org/wiki/GzipFilter">GzipFilter</a> middleware that can be used to accomplish this.</p>
</div>
<div class="section" id="using-the-hooks-plugin">
<h2>Using the hooks plugin<a class="headerlink" href="#using-the-hooks-plugin" title="Permalink to this headline"></a></h2>
<p>For example, if you want to allow Cross-Origin Resource Sharing for
the content returned by all of your URL, you can use the hook
decorator and setup a callback function:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">bottle</span> <span class="k">import</span> <span class="n">hook</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">route</span>

<span class="nd">@hook</span><span class="p">(</span><span class="s1">&#39;after_request&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">enable_cors</span><span class="p">():</span>
    <span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="p">[</span><span class="s1">&#39;Access-Control-Allow-Origin&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;*&#39;</span>

<span class="nd">@route</span><span class="p">(</span><span class="s1">&#39;/foo&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">say_foo</span><span class="p">():</span>
    <span class="k">return</span> <span class="s1">&#39;foo!&#39;</span>

<span class="nd">@route</span><span class="p">(</span><span class="s1">&#39;/bar&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">say_bar</span><span class="p">():</span>
    <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;friendly&#39;</span><span class="p">,</span> <span class="s1">&#39;content&#39;</span><span class="p">:</span> <span class="s1">&#39;Hi!&#39;</span><span class="p">}</span>
</pre></div>
</div>
<p>You can also use the <code class="docutils literal"><span class="pre">before_request</span></code> to take an action before
every function gets called.</p>
</div>
<div class="section" id="using-bottle-with-heroku">
<h2>Using Bottle with Heroku<a class="headerlink" href="#using-bottle-with-heroku" title="Permalink to this headline"></a></h2>
<p><a class="reference external" href="http://heroku.com">Heroku</a>, a popular cloud application platform now provides support
for running Python applications on their infastructure.</p>
<p>This recipe is based upon the <a class="reference external" href="http://devcenter.heroku.com/articles/quickstart">Heroku Quickstart</a>,
with Bottle specific code replacing the
<a class="reference external" href="http://devcenter.heroku.com/articles/python#write_your_app">Write Your App</a>
section of the <a class="reference external" href="http://devcenter.heroku.com/articles/python">Getting Started with Python on Heroku/Cedar</a> guide:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">bottle</span> <span class="k">import</span> <span class="n">route</span><span class="p">,</span> <span class="n">run</span>

<span class="nd">@route</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">hello_world</span><span class="p">():</span>
        <span class="k">return</span> <span class="s2">&quot;Hello World!&quot;</span>

<span class="n">run</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="s2">&quot;0.0.0.0&quot;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;PORT&quot;</span><span class="p">,</span> <span class="mi">5000</span><span class="p">)))</span>
</pre></div>
</div>
<p>Heroku&#8217;s app stack passes the port that the application needs to
listen on for requests, using the <cite>os.environ</cite> dictionary.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Recipes</a><ul>
<li><a class="reference internal" href="#keeping-track-of-sessions">Keeping track of Sessions</a></li>
<li><a class="reference internal" href="#debugging-with-style-debugging-middleware">Debugging with Style: Debugging Middleware</a></li>
<li><a class="reference internal" href="#unit-testing-bottle-applications">Unit-Testing Bottle Applications</a></li>
<li><a class="reference internal" href="#functional-testing-bottle-applications">Functional Testing Bottle Applications</a></li>
<li><a class="reference internal" href="#embedding-other-wsgi-apps">Embedding other WSGI Apps</a></li>
<li><a class="reference internal" href="#ignore-trailing-slashes">Ignore trailing slashes</a></li>
<li><a class="reference internal" href="#keep-alive-requests">Keep-alive requests</a></li>
<li><a class="reference internal" href="#gzip-compression-in-bottle">Gzip Compression in Bottle</a></li>
<li><a class="reference internal" href="#using-the-hooks-plugin">Using the hooks plugin</a></li>
<li><a class="reference internal" href="#using-bottle-with-heroku">Using Bottle with Heroku</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="index.html">Documentation overview</a><ul>
      <li>Previous: <a href="async.html" title="previous chapter">Primer to Asynchronous Applications</a></li>
      <li>Next: <a href="faq.html" title="next chapter">Frequently Asked Questions</a></li>
  </ul></li>
</ul>
</div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/recipes.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2009-2017, Marcel Hellkamp.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.4.9</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
      
      |
      <a href="_sources/recipes.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>