We have answer of your question!

100% solved queries, no empty question

Question: Python CGIHTTPServer set file types for dynamic vs static pages



This question (Python CGIHTTPServer Default Directories) details how to set paths for location of cgi-bin files for Python CGIHTTPServer. From testing this, it seems that you cannot mix .py and .html files in the same folder: in cgi-bin it processes .py files fine, but asked to serve a static html file I get - - [08/Jan/2017 10:51:22] "GET /dev.html HTTP/1.1" 200 -
Traceback (most recent call last):
   File "/usr/lib/python2.7/CGIHTTPServer.py", line 248, in run_cgi
      os.execve(scriptfile, args, env)
OSError: [Errno 8] Exec format error - - [08/Jan/2017 10:51:22] CGI script exit status 0x7f00

Is this the real intended behaviour, or am I missing something? The sparse and opaque documentation says "The class will however, run the CGI script, instead of serving it as a file, if it guesses it to be a CGI script. Only directory-based CGI are used — the other common server configuration is to treat special extensions as denoting CGI scripts."

How do I do "treat special extensions as denoting CGI scripts". What method or setting do I use, or which magic words do I utter? Or is this just an ineptly worded tip-off that I just can't do it?

I'm only using this for quick tests, and while I could restructure to separate .py and .html files I have other constraints that would make this a painful exercise.

Question author Havlock | Source




I took oryginal is_cgi() from CGIHTTPServer.py and add two elements

  • CGIHTTPServer. in CGIHTTPServer._url_collapse_path(self.path) to use it outside file CGIHTTPServer.py
  • and more important: checking extension

    if not tail.endswith('.html'):

    but it could be done better.

I didn't use

    if tail.endswith('.py'):

because server may execute scripts in other languages if you need - ie. Perl, PHP, Bash, etc.


import BaseHTTPServer
import CGIHTTPServer

class MyHandler(CGIHTTPServer.CGIHTTPRequestHandler):

    # code from oryginal CGIHTTPServer.py
    def is_cgi(self):
        #                v added `CGIHTTPServer.`
        collapsed_path = CGIHTTPServer._url_collapse_path(self.path) 
        dir_sep = collapsed_path.find('/', 1)
        head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
        if head in self.cgi_directories:
            if not tail.endswith('.html'): # <-- new line
            #if tail.endswith('.py'): # <-- new line
                self.cgi_info = head, tail
                return True
        return False

# --- test ---

MyHandler.cgi_directories = ['/']

server = BaseHTTPServer.HTTPServer(('', 8000), MyHandler)
Answer author Furas


Tickanswer.com is providing the only single recommended solution of the question Python CGIHTTPServer set file types for dynamic vs static pages under the categories i.e python , cgihttpserver , . Our team of experts filter the best solution for you.

Related Search Queries:

You may also add your answer!