Is there an alternative to RewriteRule / .htaccess for Python http.server.HTTPServer?

Can my server module (with http.server.HTTPServer ) use something like a RewriteRule to redirect all traffic to a single cgi script? I would like to be able to do what is shown here in this other question, but for my python server.

Is it possible to do something like .htaccess , or is there another way?

Also, can this be done even for a simple localhost development server?
I serve development files, for example, http: // localhost: 8000 / html / index.html , and I would like to hide the / html subfolder from the URL even in development.
How can this be achieved?

+6
source share
1 answer

You can use a custom script to initialize your server and determine the Routes in it, for example, the article proposed in this article :

Python 2:

server.py

 import os import posixpath import urllib import BaseHTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler # modify this to add additional routes ROUTES = ( <- this is the "good stuff", make aliases for your paths # [url_prefix , directory_path] ['/media', '/var/www/media'], ['', '/var/www/site'] # empty string for the 'default' match ) class RequestHandler(SimpleHTTPRequestHandler): def translate_path(self, path): """translate path given routes""" # set default root to cwd root = os.getcwd() # look up routes and set root directory accordingly for pattern, rootdir in ROUTES: if path.startswith(pattern): # found match! path = path[len(pattern):] # consume path up to pattern len root = rootdir break # normalize path and prepend root directory path = path.split('?',1)[0] path = path.split('#',1)[0] path = posixpath.normpath(urllib.unquote(path)) words = path.split('/') words = filter(None, words) path = root for word in words: drive, word = os.path.splitdrive(word) head, word = os.path.split(word) if word in (os.curdir, os.pardir): continue path = os.path.join(path, word) return path if __name__ == '__main__': BaseHTTPServer.test(RequestHandler, BaseHTTPServer.HTTPServer) 

Then run the script:

 python server.py 

Python 3:

In Python 3, the BaseHTTPServer and SimpleHTTPServer were combined into the http.server module. Therefore, you will have to modify the above script as follows:

Change

 import BaseHTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler 

to

 import http.server 

(if you do, calls should be changed to http.server.SimpleHTTPRequest , etc.)

or

 from http.server import BaseHTTPServer, SimpleHTTPServer, SimpleHTTPRequestHandler 

(the calls to this option remain the same as the original script)

then run the server script: python server.py


WHEN:

You must modify the ROUTES variable to suit your needs.
For example, you want to hide the /html folder from your URL:

 ROUTES = ( ['', '/exact/path/to/folder/html'], ['/another_url_path', '/exact/path/to/another/folder'], ... ) 

Now, if you click: http://localhost:8000/index.html , you will be taken to your home page.


Note:

This script by default will serve the contained files in the folder that is located at startup on the domain URL (for example, I have server.py in the Documents folder, and then when I run it, http: // localhost: 8000 url will load my Documents folder).
You can change this behavior by Routes (see the default comment for code) or by placing the script in the root folder of the projects and running it from there.

+4
source

Source: https://habr.com/ru/post/953197/


All Articles