/usr/share/doc/python-twisted-web/howto/web-in-60/other-request-bodies.html is in python-twisted-web 13.2.0-1ubuntu1.
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 | <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Twisted Documentation: Other Request Bodies</title>
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body bgcolor="white">
<h1 class="title">Other Request Bodies</h1>
<div class="toc"><ol/></div>
<div class="content">
<span/>
<p>The previous example demonstrated how to accept the payload of
a <code>POST</code> carrying HTML form data. What about <code>POST</code>
requests with data in some other format? Or even <code>PUT</code> requests?
Here is an example which demonstrates how to get <em>any</em> request body,
regardless of its format - using the request's
<code class="API"><a href="http://twistedmatrix.com/documents/13.2.0/api/twisted.web.iweb.IRequest.content.html" title="twisted.web.iweb.IRequest.content">content</a></code> attribute.</p>
<p>The only significant difference between this example and the previous is that
instead of accessing <code class="python">request.args</code>
in <code class="python">render_POST</code>, it
uses <code class="python">request.content</code> to get the request's body
directly:</p>
<pre class="python"><p class="py-linenumber">1
2
3
</p>...
<span class="py-src-keyword">def</span> <span class="py-src-identifier">render_POST</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
<span class="py-src-keyword">return</span> <span class="py-src-string">'<html><body>You submitted: %s</body></html>'</span> % (<span class="py-src-variable">cgi</span>.<span class="py-src-variable">escape</span>(<span class="py-src-variable">request</span>.<span class="py-src-variable">content</span>.<span class="py-src-variable">read</span>()),)
</pre>
<p><code class="python">request.content</code> is a file-like object, so the
body is read from it. The exact type may vary, so avoid relying on non-file
methods you may find (such as <code class="python">getvalue</code> when happens
to be a <code class="python">StringIO</code> instance).</p>
<p>Here's the complete source for this example - again, almost identical to the
previous <code>POST</code> example, with
only <code class="python">render_POST</code> changed:</p>
<pre class="python"><p class="py-linenumber"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">server</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Site</span>
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">web</span>.<span class="py-src-variable">resource</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Resource</span>
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
<span class="py-src-keyword">import</span> <span class="py-src-variable">cgi</span>
<span class="py-src-keyword">class</span> <span class="py-src-identifier">FormPage</span>(<span class="py-src-parameter">Resource</span>):
<span class="py-src-keyword">def</span> <span class="py-src-identifier">render_GET</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
<span class="py-src-keyword">return</span> <span class="py-src-string">'<html><body><form method="POST"><input name="the-field" type="text" /></form></body></html>'</span>
<span class="py-src-keyword">def</span> <span class="py-src-identifier">render_POST</span>(<span class="py-src-parameter">self</span>, <span class="py-src-parameter">request</span>):
<span class="py-src-keyword">return</span> <span class="py-src-string">'<html><body>You submitted: %s</body></html>'</span> % (<span class="py-src-variable">cgi</span>.<span class="py-src-variable">escape</span>(<span class="py-src-variable">request</span>.<span class="py-src-variable">content</span>.<span class="py-src-variable">read</span>()),)
<span class="py-src-variable">root</span> = <span class="py-src-variable">Resource</span>()
<span class="py-src-variable">root</span>.<span class="py-src-variable">putChild</span>(<span class="py-src-string">"form"</span>, <span class="py-src-variable">FormPage</span>())
<span class="py-src-variable">factory</span> = <span class="py-src-variable">Site</span>(<span class="py-src-variable">root</span>)
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">listenTCP</span>(<span class="py-src-number">8880</span>, <span class="py-src-variable">factory</span>)
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
</pre>
</div>
<p><a href="../index.html">Index</a></p>
<span class="version">Version: 13.2.0</span>
</body>
</html>
|