summaryrefslogtreecommitdiffstats
path: root/Doc/library/wsgiref.rst
diff options
context:
space:
mode:
authorHugo van Kemenade <hugovk@users.noreply.github.com>2022-04-29 13:47:26 (GMT)
committerGitHub <noreply@github.com>2022-04-29 13:47:26 (GMT)
commit89c6b2b8f615a1c1827a92c4582c213b1a5027fb (patch)
treef4e4996942f7d0e314d0a2e4c732d74b098aef36 /Doc/library/wsgiref.rst
parent64113a4ba801126028505c50a7383f3e9df29573 (diff)
downloadcpython-89c6b2b8f615a1c1827a92c4582c213b1a5027fb.zip
cpython-89c6b2b8f615a1c1827a92c4582c213b1a5027fb.tar.gz
cpython-89c6b2b8f615a1c1827a92c4582c213b1a5027fb.tar.bz2
bpo-36329: Remove 'make -C Doc serve' in favour of 'make -C Doc htmlview' (GH-32354)
Also updated `make -C htmlview` so it used a full path with `file://`, because the original didn't open the page (macOS). For example: ```sh cd Doc # Doesn't open anything: python3 -c "import webbrowser; webbrowser.open('build/html/index.html')" # Opens the docs page e.g. file:///Users/hugo/github/cpython/Doc/build/html/index.html : python3 -c "import os, webbrowser; webbrowser.open('file://' + os.path.realpath('build/html/index.html'))" ``` https://bugs.python.org/issue36329
Diffstat (limited to 'Doc/library/wsgiref.rst')
-rw-r--r--Doc/library/wsgiref.rst68
1 files changed, 57 insertions, 11 deletions
diff --git a/Doc/library/wsgiref.rst b/Doc/library/wsgiref.rst
index 0ca7b00..6a2d478 100644
--- a/Doc/library/wsgiref.rst
+++ b/Doc/library/wsgiref.rst
@@ -813,30 +813,76 @@ Examples
This is a working "Hello World" WSGI application::
+ """
+ Every WSGI application must have an application object - a callable
+ object that accepts two arguments. For that purpose, we're going to
+ use a function (note that you're not limited to a function, you can
+ use a class for example). The first argument passed to the function
+ is a dictionary containing CGI-style environment variables and the
+ second variable is the callable object.
+ """
from wsgiref.simple_server import make_server
- # Every WSGI application must have an application object - a callable
- # object that accepts two arguments. For that purpose, we're going to
- # use a function (note that you're not limited to a function, you can
- # use a class for example). The first argument passed to the function
- # is a dictionary containing CGI-style environment variables and the
- # second variable is the callable object.
+
def hello_world_app(environ, start_response):
- status = '200 OK' # HTTP Status
- headers = [('Content-type', 'text/plain; charset=utf-8')] # HTTP Headers
+ status = "200 OK" # HTTP Status
+ headers = [("Content-type", "text/plain; charset=utf-8")] # HTTP Headers
start_response(status, headers)
# The returned object is going to be printed
return [b"Hello World"]
- with make_server('', 8000, hello_world_app) as httpd:
+ with make_server("", 8000, hello_world_app) as httpd:
print("Serving on port 8000...")
# Serve until process is killed
httpd.serve_forever()
+
Example of a WSGI application serving the current directory, accept optional
-directory and port number (default: 8000) on the command line:
+directory and port number (default: 8000) on the command line::
+
+ """
+ Small wsgiref based web server. Takes a path to serve from and an
+ optional port number (defaults to 8000), then tries to serve files.
+ MIME types are guessed from the file names, 404 errors are raised
+ if the file is not found.
+ """
+ import mimetypes
+ import os
+ import sys
+ from wsgiref import simple_server, util
+
+
+ def app(environ, respond):
+ # Get the file name and MIME type
+ fn = os.path.join(path, environ["PATH_INFO"][1:])
+ if "." not in fn.split(os.path.sep)[-1]:
+ fn = os.path.join(fn, "index.html")
+ mime_type = mimetypes.guess_type(fn)[0]
+
+ # Return 200 OK if file exists, otherwise 404 Not Found
+ if os.path.exists(fn):
+ respond("200 OK", [("Content-Type", mime_type)])
+ return util.FileWrapper(open(fn, "rb"))
+ else:
+ respond("404 Not Found", [("Content-Type", "text/plain")])
+ return [b"not found"]
+
+
+ if __name__ == "__main__":
+ # Get the path and port from command-line arguments
+ path = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
+ port = int(sys.argv[2]) if len(sys.argv) > 2 else 8000
+
+ # Make and start the server until control-c
+ httpd = simple_server.make_server("", port, app)
+ print(f"Serving {path} on port {port}, control-C to stop")
+ try:
+ httpd.serve_forever()
+ except KeyboardInterrupt:
+ print("Shutting down.")
+ httpd.server_close()
+
-.. literalinclude:: ../../Tools/scripts/serve.py