Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(14)

Delta Between Two Patch Sets: Doc/library/wsgiref.rst

Issue 26404: socketserver context manager
Left Patch Set: Created 4 years, 1 month ago
Right Patch Set: Created 4 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
LEFTRIGHT
(no file at all)
1 :mod:`wsgiref` --- WSGI Utilities and Reference Implementation 1 :mod:`wsgiref` --- WSGI Utilities and Reference Implementation
2 ============================================================== 2 ==============================================================
3 3
4 .. module:: wsgiref 4 .. module:: wsgiref
5 :synopsis: WSGI Utilities and Reference Implementation. 5 :synopsis: WSGI Utilities and Reference Implementation.
6 .. moduleauthor:: Phillip J. Eby <pje@telecommunity.com> 6 .. moduleauthor:: Phillip J. Eby <pje@telecommunity.com>
7 .. sectionauthor:: Phillip J. Eby <pje@telecommunity.com> 7 .. sectionauthor:: Phillip J. Eby <pje@telecommunity.com>
8 8
9 9
10 The Web Server Gateway Interface (WSGI) is a standard interface between web 10 The Web Server Gateway Interface (WSGI) is a standard interface between web
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 124
125 status = '200 OK' 125 status = '200 OK'
126 headers = [('Content-type', 'text/plain; charset=utf-8')] 126 headers = [('Content-type', 'text/plain; charset=utf-8')]
127 127
128 start_response(status, headers) 128 start_response(status, headers)
129 129
130 ret = [("%s: %s\n" % (key, value)).encode("utf-8") 130 ret = [("%s: %s\n" % (key, value)).encode("utf-8")
131 for key, value in environ.items()] 131 for key, value in environ.items()]
132 return ret 132 return ret
133 133
134 httpd = make_server('', 8000, simple_app) 134 with make_server('', 8000, simple_app) as httpd:
135 print("Serving on port 8000...") 135 print("Serving on port 8000...")
136 httpd.serve_forever() 136 httpd.serve_forever()
137 137
138 138
139 In addition to the environment functions above, the :mod:`wsgiref.util` module 139 In addition to the environment functions above, the :mod:`wsgiref.util` module
140 also provides these miscellaneous utilities: 140 also provides these miscellaneous utilities:
141 141
142 142
143 .. function:: is_hop_by_hop(header_name) 143 .. function:: is_hop_by_hop(header_name)
144 144
145 Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header, as defined b y 145 Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header, as defined b y
146 :rfc:`2616`. 146 :rfc:`2616`.
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 276
277 Create a new WSGI server listening on *host* and *port*, accepting connection s 277 Create a new WSGI server listening on *host* and *port*, accepting connection s
278 for *app*. The return value is an instance of the supplied *server_class*, a nd 278 for *app*. The return value is an instance of the supplied *server_class*, a nd
279 will process requests using the specified *handler_class*. *app* must be a W SGI 279 will process requests using the specified *handler_class*. *app* must be a W SGI
280 application object, as defined by :pep:`3333`. 280 application object, as defined by :pep:`3333`.
281 281
282 Example usage:: 282 Example usage::
283 283
284 from wsgiref.simple_server import make_server, demo_app 284 from wsgiref.simple_server import make_server, demo_app
285 285
286 httpd = make_server('', 8000, demo_app) 286 with make_server('', 8000, demo_app) as httpd:
287 print("Serving HTTP on port 8000...") 287 print("Serving HTTP on port 8000...")
288 288
289 # Respond to requests until process is killed 289 # Respond to requests until process is killed
290 httpd.serve_forever() 290 httpd.serve_forever()
291 291
292 # Alternative: serve one request, then exit 292 # Alternative: serve one request, then exit
293 httpd.handle_request() 293 httpd.handle_request()
294 294
295 295
296 .. function:: demo_app(environ, start_response) 296 .. function:: demo_app(environ, start_response)
297 297
298 This function is a small but complete WSGI application that returns a text pa ge 298 This function is a small but complete WSGI application that returns a text pa ge
299 containing the message "Hello world!" and a list of the key/value pairs provi ded 299 containing the message "Hello world!" and a list of the key/value pairs provi ded
300 in the *environ* parameter. It's useful for verifying that a WSGI server (su ch 300 in the *environ* parameter. It's useful for verifying that a WSGI server (su ch
301 as :mod:`wsgiref.simple_server`) is able to run a simple WSGI application 301 as :mod:`wsgiref.simple_server`) is able to run a simple WSGI application
302 correctly. 302 correctly.
303 303
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 headers = [('Content-type', 'text/plain')] # HTTP Headers 423 headers = [('Content-type', 'text/plain')] # HTTP Headers
424 start_response(status, headers) 424 start_response(status, headers)
425 425
426 # This is going to break because we need to return a list, and 426 # This is going to break because we need to return a list, and
427 # the validator is going to inform us 427 # the validator is going to inform us
428 return b"Hello World" 428 return b"Hello World"
429 429
430 # This is the application wrapped in a validator 430 # This is the application wrapped in a validator
431 validator_app = validator(simple_app) 431 validator_app = validator(simple_app)
432 432
433 httpd = make_server('', 8000, validator_app) 433 with make_server('', 8000, validator_app) as httpd:
434 print("Listening on port 8000....") 434 print("Listening on port 8000....")
435 httpd.serve_forever() 435 httpd.serve_forever()
436 436
437 437
438 :mod:`wsgiref.handlers` -- server/gateway base classes 438 :mod:`wsgiref.handlers` -- server/gateway base classes
439 ------------------------------------------------------ 439 ------------------------------------------------------
440 440
441 .. module:: wsgiref.handlers 441 .. module:: wsgiref.handlers
442 :synopsis: WSGI server/gateway base classes. 442 :synopsis: WSGI server/gateway base classes.
443 443
444 444
445 This module provides base handler classes for implementing WSGI servers and 445 This module provides base handler classes for implementing WSGI servers and
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 # is a dictionary containing CGI-style environment variables and the 762 # is a dictionary containing CGI-style environment variables and the
763 # second variable is the callable object (see PEP 333). 763 # second variable is the callable object (see PEP 333).
764 def hello_world_app(environ, start_response): 764 def hello_world_app(environ, start_response):
765 status = '200 OK' # HTTP Status 765 status = '200 OK' # HTTP Status
766 headers = [('Content-type', 'text/plain; charset=utf-8')] # HTTP Headers 766 headers = [('Content-type', 'text/plain; charset=utf-8')] # HTTP Headers
767 start_response(status, headers) 767 start_response(status, headers)
768 768
769 # The returned object is going to be printed 769 # The returned object is going to be printed
770 return [b"Hello World"] 770 return [b"Hello World"]
771 771
772 httpd = make_server('', 8000, hello_world_app) 772 with make_server('', 8000, hello_world_app) as httpd:
773 print("Serving on port 8000...") 773 print("Serving on port 8000...")
774 774
775 # Serve until process is killed 775 # Serve until process is killed
776 httpd.serve_forever() 776 httpd.serve_forever()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+