@@ -813,30 +813,76 @@ Examples
813
813
814
814
This is a working "Hello World" WSGI application::
815
815
816
+ """
817
+ Every WSGI application must have an application object - a callable
818
+ object that accepts two arguments. For that purpose, we're going to
819
+ use a function (note that you're not limited to a function, you can
820
+ use a class for example). The first argument passed to the function
821
+ is a dictionary containing CGI-style environment variables and the
822
+ second variable is the callable object.
823
+ """
816
824
from wsgiref.simple_server import make_server
817
825
818
- # Every WSGI application must have an application object - a callable
819
- # object that accepts two arguments. For that purpose, we're going to
820
- # use a function (note that you're not limited to a function, you can
821
- # use a class for example). The first argument passed to the function
822
- # is a dictionary containing CGI-style environment variables and the
823
- # second variable is the callable object.
826
+
824
827
def hello_world_app(environ, start_response):
825
- status = ' 200 OK' # HTTP Status
826
- headers = [(' Content-type', ' text/plain; charset=utf-8' )] # HTTP Headers
828
+ status = " 200 OK" # HTTP Status
829
+ headers = [(" Content-type", " text/plain; charset=utf-8" )] # HTTP Headers
827
830
start_response(status, headers)
828
831
829
832
# The returned object is going to be printed
830
833
return [b"Hello World"]
831
834
832
- with make_server('' , 8000, hello_world_app) as httpd:
835
+ with make_server("" , 8000, hello_world_app) as httpd:
833
836
print("Serving on port 8000...")
834
837
835
838
# Serve until process is killed
836
839
httpd.serve_forever()
837
840
838
841
842
+
839
843
Example of a WSGI application serving the current directory, accept optional
840
- directory and port number (default: 8000) on the command line:
844
+ directory and port number (default: 8000) on the command line::
845
+
846
+ """
847
+ Small wsgiref based web server. Takes a path to serve from and an
848
+ optional port number (defaults to 8000), then tries to serve files.
849
+ MIME types are guessed from the file names, 404 errors are raised
850
+ if the file is not found.
851
+ """
852
+ import mimetypes
853
+ import os
854
+ import sys
855
+ from wsgiref import simple_server, util
856
+
857
+
858
+ def app(environ, respond):
859
+ # Get the file name and MIME type
860
+ fn = os.path.join(path, environ["PATH_INFO"][1:])
861
+ if "." not in fn.split(os.path.sep)[-1]:
862
+ fn = os.path.join(fn, "index.html")
863
+ mime_type = mimetypes.guess_type(fn)[0]
864
+
865
+ # Return 200 OK if file exists, otherwise 404 Not Found
866
+ if os.path.exists(fn):
867
+ respond("200 OK", [("Content-Type", mime_type)])
868
+ return util.FileWrapper(open(fn, "rb"))
869
+ else:
870
+ respond("404 Not Found", [("Content-Type", "text/plain")])
871
+ return [b"not found"]
872
+
873
+
874
+ if __name__ == "__main__":
875
+ # Get the path and port from command-line arguments
876
+ path = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
877
+ port = int(sys.argv[2]) if len(sys.argv) > 2 else 8000
878
+
879
+ # Make and start the server until control-c
880
+ httpd = simple_server.make_server("", port, app)
881
+ print(f"Serving {path} on port {port}, control-C to stop")
882
+ try:
883
+ httpd.serve_forever()
884
+ except KeyboardInterrupt:
885
+ print("Shutting down.")
886
+ httpd.server_close()
887
+
841
888
842
- .. literalinclude :: ../../Tools/scripts/serve.py
0 commit comments