/usr/share/doc/python-webob-doc/file-example.html is in python-webob-doc 1:1.7.3-2fakesync1.
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 | <!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>WebOb File-Serving Example — WebOb 1.7.3 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: '1.7.3',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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="next" title="Wiki Example" href="wiki-example.html" />
<link rel="prev" title="webob -- Request/Response objects" href="api/webob.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>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="webob-file-serving-example">
<h1>WebOb File-Serving Example<a class="headerlink" href="#webob-file-serving-example" title="Permalink to this headline">ΒΆ</a></h1>
<p>This document shows how you can make a static-file-serving application
using WebOb. We'll quickly build this up from minimal functionality
to a high-quality file serving application.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Starting from 1.2b4, WebOb ships with a <a class="reference internal" href="api/static.html#module-webob.static" title="webob.static"><code class="xref py py-mod docutils literal"><span class="pre">webob.static</span></code></a> module
which implements a <a class="reference internal" href="api/static.html#webob.static.FileApp" title="webob.static.FileApp"><code class="xref py py-class docutils literal"><span class="pre">webob.static.FileApp</span></code></a> WSGI application similar to the
one described below.</p>
<p class="last">This document stays as a didactic example how to serve files with WebOb, but
you should consider using applications from <a class="reference internal" href="api/static.html#module-webob.static" title="webob.static"><code class="xref py py-mod docutils literal"><span class="pre">webob.static</span></code></a> in
production.</p>
</div>
<p>First we'll setup a really simple shim around our application, which
we can use as we improve our application:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">webob</span> <span class="kn">import</span> <span class="n">Request</span><span class="p">,</span> <span class="n">Response</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">os</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">FileApp</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">res</span> <span class="o">=</span> <span class="n">make_response</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">res</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">mimetypes</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">get_mimetype</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">type</span><span class="p">,</span> <span class="n">encoding</span> <span class="o">=</span> <span class="n">mimetypes</span><span class="o">.</span><span class="n">guess_type</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
<span class="gp">... </span> <span class="c1"># We'll ignore encoding, even though we shouldn't really</span>
<span class="gp">... </span> <span class="k">return</span> <span class="nb">type</span> <span class="ow">or</span> <span class="s1">'application/octet-stream'</span>
</pre></div>
</div>
<p>Now we can make different definitions of <code class="docutils literal"><span class="pre">make_response</span></code>. The
simplest version:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">make_response</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">res</span> <span class="o">=</span> <span class="n">Response</span><span class="p">(</span><span class="n">content_type</span><span class="o">=</span><span class="n">get_mimetype</span><span class="p">(</span><span class="n">filename</span><span class="p">))</span>
<span class="gp">... </span> <span class="n">res</span><span class="o">.</span><span class="n">body</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">res</span>
</pre></div>
</div>
<p>We'll test it out with a file <code class="docutils literal"><span class="pre">test-file.txt</span></code> in the WebOb doc directory,
which has the following content:</p>
<div class="highlight-text"><div class="highlight"><pre><span></span>This is a test. Hello test people!
</pre></div>
</div>
<p>Let's give it a shot:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">fn</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">doc_dir</span><span class="p">,</span> <span class="s1">'file-example-code/test-file.txt'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">open</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="go">'This is a test. Hello test people!'</span>
<span class="gp">>>> </span><span class="n">app</span> <span class="o">=</span> <span class="n">FileApp</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">req</span> <span class="o">=</span> <span class="n">Request</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">req</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="go">200 OK</span>
<span class="go">Content-Type: text/plain; charset=UTF-8</span>
<span class="go">Content-Length: 35</span>
<span class="go">This is a test. Hello test people!</span>
</pre></div>
</div>
<p>Well, that worked. But it's not a very fancy object. First, it reads
everything into memory, and that's bad. We'll create an iterator instead:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">FileIterable</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">FileIterator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">FileIterator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">chunk_size</span> <span class="o">=</span> <span class="mi">4096</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">fileobj</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">chunk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fileobj</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">chunk_size</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">if</span> <span class="ow">not</span> <span class="n">chunk</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">StopIteration</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">chunk</span>
<span class="gp">... </span> <span class="n">__next__</span> <span class="o">=</span> <span class="nb">next</span> <span class="c1"># py3 compat</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">make_response</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">res</span> <span class="o">=</span> <span class="n">Response</span><span class="p">(</span><span class="n">content_type</span><span class="o">=</span><span class="n">get_mimetype</span><span class="p">(</span><span class="n">filename</span><span class="p">))</span>
<span class="gp">... </span> <span class="n">res</span><span class="o">.</span><span class="n">app_iter</span> <span class="o">=</span> <span class="n">FileIterable</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">res</span><span class="o">.</span><span class="n">content_length</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getsize</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">res</span>
</pre></div>
</div>
<p>And testing:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">req</span> <span class="o">=</span> <span class="n">Request</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">req</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="go">200 OK</span>
<span class="go">Content-Type: text/plain; charset=UTF-8</span>
<span class="go">Content-Length: 35</span>
<span class="go">This is a test. Hello test people!</span>
</pre></div>
</div>
<p>Well, that doesn't <em>look</em> different, but lets <em>imagine</em> that it's
different because we know we changed some code. Now to add some basic
metadata to the response:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">make_response</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">res</span> <span class="o">=</span> <span class="n">Response</span><span class="p">(</span><span class="n">content_type</span><span class="o">=</span><span class="n">get_mimetype</span><span class="p">(</span><span class="n">filename</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">conditional_response</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">res</span><span class="o">.</span><span class="n">app_iter</span> <span class="o">=</span> <span class="n">FileIterable</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">res</span><span class="o">.</span><span class="n">content_length</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getsize</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">res</span><span class="o">.</span><span class="n">last_modified</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getmtime</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">res</span><span class="o">.</span><span class="n">etag</span> <span class="o">=</span> <span class="s1">'</span><span class="si">%s</span><span class="s1">-</span><span class="si">%s</span><span class="s1">-</span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getmtime</span><span class="p">(</span><span class="n">filename</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getsize</span><span class="p">(</span><span class="n">filename</span><span class="p">),</span> <span class="nb">hash</span><span class="p">(</span><span class="n">filename</span><span class="p">))</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">res</span>
</pre></div>
</div>
<p>Now, with <code class="docutils literal"><span class="pre">conditional_response</span></code> on, and with <code class="docutils literal"><span class="pre">last_modified</span></code> and
<code class="docutils literal"><span class="pre">etag</span></code> set, we can do conditional requests:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">req</span> <span class="o">=</span> <span class="n">Request</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res</span> <span class="o">=</span> <span class="n">req</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">res</span>
<span class="go">200 OK</span>
<span class="go">Content-Type: text/plain; charset=UTF-8</span>
<span class="go">Content-Length: 35</span>
<span class="go">Last-Modified: ... GMT</span>
<span class="go">ETag: ...-...</span>
<span class="go">This is a test. Hello test people!</span>
<span class="gp">>>> </span><span class="n">req2</span> <span class="o">=</span> <span class="n">Request</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">req2</span><span class="o">.</span><span class="n">if_none_match</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">etag</span>
<span class="gp">>>> </span><span class="n">req2</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="go"><Response ... 304 Not Modified></span>
<span class="gp">>>> </span><span class="n">req3</span> <span class="o">=</span> <span class="n">Request</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">req3</span><span class="o">.</span><span class="n">if_modified_since</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">last_modified</span>
<span class="gp">>>> </span><span class="n">req3</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="go"><Response ... 304 Not Modified></span>
</pre></div>
</div>
<p>We can even do Range requests, but it will currently involve iterating
through the file unnecessarily. When there's a range request (and you
set <code class="docutils literal"><span class="pre">conditional_response=True</span></code>) the application will satisfy that
request. But with an arbitrary iterator the only way to do that is to
run through the beginning of the iterator until you get to the chunk
that the client asked for. We can do better because we can use
<code class="docutils literal"><span class="pre">fileobj.seek(pos)</span></code> to move around the file much more efficiently.</p>
<p>So we'll add an extra method, <code class="docutils literal"><span class="pre">app_iter_range</span></code>, that <code class="docutils literal"><span class="pre">Response</span></code>
looks for:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">FileIterable</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">start</span> <span class="o">=</span> <span class="n">start</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">stop</span> <span class="o">=</span> <span class="n">stop</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">FileIterator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">stop</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">app_iter_range</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">FileIterator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">chunk_size</span> <span class="o">=</span> <span class="mi">4096</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">fileobj</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">if</span> <span class="n">start</span><span class="p">:</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">fileobj</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="n">start</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">if</span> <span class="n">stop</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">length</span> <span class="o">=</span> <span class="n">stop</span> <span class="o">-</span> <span class="n">start</span>
<span class="gp">... </span> <span class="k">else</span><span class="p">:</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">length</span> <span class="o">=</span> <span class="bp">None</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">length</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">length</span> <span class="o"><=</span> <span class="mi">0</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">StopIteration</span>
<span class="gp">... </span> <span class="n">chunk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fileobj</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">chunk_size</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">if</span> <span class="ow">not</span> <span class="n">chunk</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">StopIteration</span>
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">length</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">length</span> <span class="o">-=</span> <span class="nb">len</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">length</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span>
<span class="gp">... </span> <span class="c1"># Chop off the extra:</span>
<span class="gp">... </span> <span class="n">chunk</span> <span class="o">=</span> <span class="n">chunk</span><span class="p">[:</span><span class="bp">self</span><span class="o">.</span><span class="n">length</span><span class="p">]</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">chunk</span>
<span class="gp">... </span> <span class="n">__next__</span> <span class="o">=</span> <span class="nb">next</span> <span class="c1"># py3 compat</span>
</pre></div>
</div>
<p>Now we'll test it out:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">req</span> <span class="o">=</span> <span class="n">Request</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res</span> <span class="o">=</span> <span class="n">req</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">req2</span> <span class="o">=</span> <span class="n">Request</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="c1"># Re-fetch the first 5 bytes:</span>
<span class="gp">>>> </span><span class="n">req2</span><span class="o">.</span><span class="n">range</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res2</span> <span class="o">=</span> <span class="n">req2</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res2</span>
<span class="go"><Response ... 206 Partial Content></span>
<span class="gp">>>> </span><span class="c1"># Let's check it's our custom class:</span>
<span class="gp">>>> </span><span class="n">res2</span><span class="o">.</span><span class="n">app_iter</span>
<span class="go"><FileIterable object at ...></span>
<span class="gp">>>> </span><span class="n">res2</span><span class="o">.</span><span class="n">body</span>
<span class="go">'This '</span>
<span class="gp">>>> </span><span class="c1"># Now, conditional range support:</span>
<span class="gp">>>> </span><span class="n">req3</span> <span class="o">=</span> <span class="n">Request</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">req3</span><span class="o">.</span><span class="n">if_range</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">etag</span>
<span class="gp">>>> </span><span class="n">req3</span><span class="o">.</span><span class="n">range</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">req3</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="go"><Response ... 206 Partial Content></span>
<span class="gp">>>> </span><span class="n">req3</span><span class="o">.</span><span class="n">if_range</span> <span class="o">=</span> <span class="s1">'invalid-etag'</span>
<span class="gp">>>> </span><span class="n">req3</span><span class="o">.</span><span class="n">get_response</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
<span class="go"><Response ... 200 OK></span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="api/webob.html" title="previous chapter"><code class="docutils literal"><span class="pre">webob</span></code> -- Request/Response objects</a></li>
<li>Next: <a href="wiki-example.html" title="next chapter">Wiki Example</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/file-example.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">
©2017, Ian Bicking and contributors.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.5</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
|
<a href="_sources/file-example.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>
|