From 756332d261a163fb67f26fa7536453e65006bec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Tue, 26 Mar 2019 15:07:45 +0100 Subject: [PATCH 1/7] bpo-36345: Include the code from Tools/scripts/serve.py for the wsgiref-base web server example --- Doc/library/wsgiref.rst | 33 ++----------------- .../2019-03-26-14-58-34.bpo-36345.r2stx3.rst | 3 ++ 2 files changed, 6 insertions(+), 30 deletions(-) create mode 100644 Misc/NEWS.d/next/Documentation/2019-03-26-14-58-34.bpo-36345.r2stx3.rst diff --git a/Doc/library/wsgiref.rst b/Doc/library/wsgiref.rst index b85ec53c8ae536..ce4158e7b4a45f 100644 --- a/Doc/library/wsgiref.rst +++ b/Doc/library/wsgiref.rst @@ -783,33 +783,6 @@ This is a working "Hello World" WSGI application:: httpd.serve_forever() -Example of a 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 sys - import os - import mimetypes - from wsgiref import simple_server, util - - def app(environ, respond): - 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') - type = mimetypes.guess_type(fn)[0] - - if os.path.exists(fn): - respond('200 OK', [('Content-Type', type)]) - return util.FileWrapper(open(fn, "rb")) - else: - respond('404 Not Found', [('Content-Type', 'text/plain')]) - return [b'not found'] - - path = sys.argv[1] - port = int(sys.argv[2]) if len(sys.argv) > 2 else 8000 - with simple_server.make_server('', port, app) as httpd: - print("Serving {} on port {}, control-C to stop".format(path, port)) - - # Serve until process is killed - httpd.serve_forever() +Example of a small wsgiref-based web server + +.. literalinclude:: ../../Tools/scripts/serve.py diff --git a/Misc/NEWS.d/next/Documentation/2019-03-26-14-58-34.bpo-36345.r2stx3.rst b/Misc/NEWS.d/next/Documentation/2019-03-26-14-58-34.bpo-36345.r2stx3.rst new file mode 100644 index 00000000000000..41f8406824b545 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2019-03-26-14-58-34.bpo-36345.r2stx3.rst @@ -0,0 +1,3 @@ +Avoid the duplication of code from ``Tools/scripts/serve.py`` in using the +``.. literalinclude::`` directive for the basic wsgiref-based web server in the +documentation of :mod:`wsgiref`. Contributed by Stéphane Wirtel. From 74fd34c4a003b2ded331afd5b47fc2aeff616048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Tue, 26 Mar 2019 17:01:25 +0100 Subject: [PATCH 2/7] Fix the blurb entry --- .../next/Documentation/2019-03-26-14-58-34.bpo-36345.r2stx3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Documentation/2019-03-26-14-58-34.bpo-36345.r2stx3.rst b/Misc/NEWS.d/next/Documentation/2019-03-26-14-58-34.bpo-36345.r2stx3.rst index 41f8406824b545..bbecc947cafa74 100644 --- a/Misc/NEWS.d/next/Documentation/2019-03-26-14-58-34.bpo-36345.r2stx3.rst +++ b/Misc/NEWS.d/next/Documentation/2019-03-26-14-58-34.bpo-36345.r2stx3.rst @@ -1,3 +1,3 @@ Avoid the duplication of code from ``Tools/scripts/serve.py`` in using the -``.. literalinclude::`` directive for the basic wsgiref-based web server in the +:rst:dir:`literalinclude` directive for the basic wsgiref-based web server in the documentation of :mod:`wsgiref`. Contributed by Stéphane Wirtel. From c9388132857b78f7f26cb2200b393693bef34072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Mon, 15 Apr 2019 14:11:46 +0200 Subject: [PATCH 3/7] Example of a WSGI application serving the current directory, accept optional directory and port number on the command line. --- Tools/scripts/serve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/scripts/serve.py b/Tools/scripts/serve.py index dae21f2260ff13..a69ac04f65ff90 100755 --- a/Tools/scripts/serve.py +++ b/Tools/scripts/serve.py @@ -25,7 +25,7 @@ def app(environ, respond): return [b'not found'] if __name__ == '__main__': - path = sys.argv[1] + path = sys.argv[1] if len(sys.argv) > 1 else os.getcwd() port = int(sys.argv[2]) if len(sys.argv) > 2 else 8000 httpd = simple_server.make_server('', port, app) print("Serving {} on port {}, control-C to stop".format(path, port)) From a3dbd359a2c923a0b87c0e89f6509fc97e076924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Mon, 15 Apr 2019 14:22:49 +0200 Subject: [PATCH 4/7] Update the description of the example --- Doc/library/wsgiref.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/wsgiref.rst b/Doc/library/wsgiref.rst index ce4158e7b4a45f..3136b17fa35d5c 100644 --- a/Doc/library/wsgiref.rst +++ b/Doc/library/wsgiref.rst @@ -783,6 +783,7 @@ This is a working "Hello World" WSGI application:: httpd.serve_forever() -Example of a small wsgiref-based web server +Example of a WSGI application serving the current directory, accept optional +directory and port number on the command line: .. literalinclude:: ../../Tools/scripts/serve.py From 530ed32e1f881c50f3d438c60b7187754e9ce317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Mon, 15 Apr 2019 21:38:27 +0200 Subject: [PATCH 5/7] Add the default port in the doc --- Doc/library/wsgiref.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/wsgiref.rst b/Doc/library/wsgiref.rst index 3136b17fa35d5c..ec5136742fa2c4 100644 --- a/Doc/library/wsgiref.rst +++ b/Doc/library/wsgiref.rst @@ -784,6 +784,6 @@ This is a working "Hello World" WSGI application:: Example of a WSGI application serving the current directory, accept optional -directory and port number on the command line: +directory and port number (default: 8000) on the command line: .. literalinclude:: ../../Tools/scripts/serve.py From 2b1fa7299af030d8e309fca77c1e9bd4ba060f57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Mon, 15 Apr 2019 21:45:55 +0200 Subject: [PATCH 6/7] Close the resources for the httpd server --- Tools/scripts/serve.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Tools/scripts/serve.py b/Tools/scripts/serve.py index a69ac04f65ff90..529d68182fd15f 100755 --- a/Tools/scripts/serve.py +++ b/Tools/scripts/serve.py @@ -33,3 +33,4 @@ def app(environ, respond): httpd.serve_forever() except KeyboardInterrupt: print("\b\bShutting down.") + httpd.server_close() From a7db659422bddd7b0f90a5be7d8d2e8706d59197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Mon, 15 Apr 2019 21:47:54 +0200 Subject: [PATCH 7/7] Remove \b\b for the portability --- Tools/scripts/serve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/scripts/serve.py b/Tools/scripts/serve.py index 529d68182fd15f..7ac9c105078329 100755 --- a/Tools/scripts/serve.py +++ b/Tools/scripts/serve.py @@ -32,5 +32,5 @@ def app(environ, respond): try: httpd.serve_forever() except KeyboardInterrupt: - print("\b\bShutting down.") + print("Shutting down.") httpd.server_close()