-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathstdlib.html
More file actions
200 lines (181 loc) · 9.08 KB
/
stdlib.html
File metadata and controls
200 lines (181 loc) · 9.08 KB
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
<!DOCTYPE html>
<html>
<head>
<title>stdlib.py</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="dycco.css">
</head>
<body>
<div id="container">
<div id="background"></div>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th class="docs">
<h1>stdlib.py</h1>
</th>
<th class="code">
</th>
</tr>
</thead>
<tbody>
<tr id="section-2">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Python-standard reference servers for development use.</p>
</td>
<td class="code">
<div class="highlight"><pre>
</pre></div>
</td>
</tr>
<tr id="section-5">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<h2>Imports</h2>
</td>
<td class="code">
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span><span class="p">,</span> <span class="n">print_function</span>
<span class="kn">from</span> <span class="nn">wsgiref.handlers</span> <span class="kn">import</span> <span class="n">CGIHandler</span>
<span class="kn">from</span> <span class="nn">wsgiref.simple_server</span> <span class="kn">import</span> <span class="n">make_server</span>
</pre></div>
</td>
</tr>
<tr id="section-13">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<h2>Production Warning</h2>
</td>
<td class="code">
<div class="highlight"><pre>
</pre></div>
</td>
</tr>
<tr id="section-15">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>We let people know it's a bad idea to use these in production.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="k">if</span> <span class="ow">not</span> <span class="n">__debug__</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">"Use of standard library reference servers in production is discouraged."</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr id="section-21">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<h2>Server Adapters</h2>
</td>
<td class="code">
<div class="highlight"><pre>
</pre></div>
</td>
</tr>
<tr id="section-22">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Python-standard WSGI-HTTP server for testing purposes.</p>
<p>The additional work performed here is to match the default startup output of "waitress".</p>
<p>This is not a production quality interface and will be have badly under load.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="k">def</span> <span class="nf">simple</span><span class="p">(</span><span class="n">application</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s">'127.0.0.1'</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">8080</span><span class="p">):</span>
</pre></div>
</td>
</tr>
<tr id="section-31">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<p>Try to be handy as many terminals allow clicking links.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="k">print</span><span class="p">(</span><span class="s">"serving on http://{0}:{1}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">))</span>
</pre></div>
</td>
</tr>
<tr id="section-34">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<p>Bind and launch the server; this is a blocking operation.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="n">make_server</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">port</span><span class="p">),</span> <span class="n">application</span><span class="p">)</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
</pre></div>
</td>
</tr>
<tr id="section-37">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<p>Python-standard WSGI-CGI server for testing purposes.</p>
<p>This is not a production quality interface and will behave badly under load. Python-as-CGI is not a very good way
to deploy any application. (Startup/shutdown on every request is a PHP problem.) This <em>can</em> be useful as a
diagnostic tool in development, however.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="k">def</span> <span class="nf">cgi</span><span class="p">(</span><span class="n">application</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">__debug__</span><span class="p">:</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">"Interactive debugging and other persistence-based processes will not work."</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr id="section-49">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-49">¶</a>
</div>
<p>Instantiate the handler and begin bridging the application.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="n">CGIHandler</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">application</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr id="section-52">
<td class="docs">
<div class="pilwrap">
<a class="pilcrow" href="#section-52">¶</a>
</div>
<p>A specialized version of the reference WSGI-CGI server to adapt to Microsoft IIS quirks.</p>
<p>This is not a production quality interface and will behave badly under load.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="k">def</span> <span class="nf">iiscgi</span><span class="p">(</span><span class="n">application</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">wsgiref.handlers</span> <span class="kn">import</span> <span class="n">IISCGIHandler</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"Python 3.2 or newer is required."</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">__debug__</span><span class="p">:</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">"Interactive debugging and other persistence-based processes will not work."</span><span class="p">)</span>
<span class="n">IISCGIHandler</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">application</span><span class="p">)</span>
</pre></div>
</td>
</tr>
</tbody>
</table>
<footer>
Generated by <b><a href="http://mccutchen.github.com/dycco/">Dycco</a></b>.
Last updated <b>25 Apr 2016</b>.
</footer>
</div>
</body>
</html>