Python CGIHTTPServer Default Directories

前端 未结 3 970
时光取名叫无心
时光取名叫无心 2021-01-19 15:58

I\'ve got the following minimal code for a CGI-handling HTTP server, derived from several examples on the inner-tubes:

#!/usr/bin/env python

import BaseHTTP         


        
相关标签:
3条回答
  • 2021-01-19 16:44

    Here is how you would make every .py file on the server a cgi file (you probably don't want that for production/a public server ;):

    import BaseHTTPServer
    import CGIHTTPServer
    import cgitb; cgitb.enable()
    
    server = BaseHTTPServer.HTTPServer
    
    # Treat everything as a cgi file, i.e.
    # `handler.cgi_directories = ["*"]` but that is not defined, so we need
    class Handler(CGIHTTPServer.CGIHTTPRequestHandler):  
      def is_cgi(self):
        self.cgi_info = '', self.path[1:]
        return True
    
    httpd = server(("", 9006), Handler)
    httpd.serve_forever()
    
    0 讨论(0)
  • 2021-01-19 16:48

    My suspicions were correct. The examples from which this code is derived showed the wrong way to set the default directory to be the same directory in which the server script resides. To set the default directory in this way, use:

    handler.cgi_directories = ["/"]
    

    Caution: This opens up potentially huge security holes if you're not behind any kind of a firewall. This is only an instructive example. Use only with extreme care.

    0 讨论(0)
  • 2021-01-19 16:56

    The solution doesn't seem to work (at least for me) if the .cgi_directories requires multiple layers of subdirectories ( ['/db/cgi-bin'] for instance). Subclassing the server and changing the is_cgi def seemed to work. Here's what I added/substituted in your script:

    from CGIHTTPServer import _url_collapse_path
    class MyCGIHTTPServer(CGIHTTPServer.CGIHTTPRequestHandler):  
      def is_cgi(self):
        collapsed_path = _url_collapse_path(self.path)
        for path in self.cgi_directories:
            if path in collapsed_path:
                dir_sep_index = collapsed_path.rfind(path) + len(path)
                head, tail = collapsed_path[:dir_sep_index], collapsed_path[dir_sep_index + 1:]
                self.cgi_info = head, tail
                return True
        return False
    
    server = BaseHTTPServer.HTTPServer
    handler = MyCGIHTTPServer
    
    0 讨论(0)
提交回复
热议问题