1

这个问题(Python CGIHTTPServer Default Directories)详细介绍了如何为 Python CGIHTTPServer 的 cgi-bin 文件的位置设置路径。通过测试,您似乎不能在同一个文件夹中混合 .py 和 .html 文件:在 cgi-bin 中,它可以很好地处理 .py 文件,但要求提供我得到的静态 html 文件

127.0.0.1 - - [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
127.0.0.1 - - [08/Jan/2017 10:51:22] CGI script exit status 0x7f00

这是真正的预期行为,还是我错过了什么?稀疏和不透明的文档说“但是,如果该类猜测它是 CGI 脚本,它将运行 CGI 脚本,而不是将其作为文件提供。仅使用基于目录的 CGI - 另一个常见的服务器配置是将特殊扩展视为表示 CGI 脚本。”

如何“将特殊扩展视为表示 CGI 脚本”。我使用什么方法或设置,或者我说什么神奇的词?或者这只是我无法做到的措辞不当的提示?

我只是将它用于快速测试,虽然我可以重组为单独的 .py 和 .html 文件,但我还有其他限制会使这成为一个痛苦的练习。

4

2 回答 2

3

我从原始is_cgi()元素中CGIHTTPServer.py添加了两个元素

  • CGIHTTPServer.CGIHTTPServer._url_collapse_path(self.path)文件外使用它CGIHTTPServer.py
  • 更重要的是:检查扩展

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

    但它可以做得更好。

我没用

    if tail.endswith('.py'):

因为如果您需要,服务器可能会执行其他语言的脚本 - 即。Perl, PHP,Bash

代码:

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)
server.serve_forever()
于 2017-01-08T13:10:02.147 回答
2

您需要检测请求的文件类型(py/cgi 或静态文件)。模仿类型可能会有所帮助。当请求静态文件时,您可以调用另一个 cgi 脚本来传递您的静态文件。顺便提一句。您应该使用 wsgi 而不是过时的 cgi。

我修改了我得到的一些旧代码(py2.7) - 这非常难看,我从未使用过它 - 但是当你在'handler.cgi_directory'中放置一个静态文件'dev.html'时,它应该由static.py提供服务。

服务器.py:

#!/usr/bin/python2
import BaseHTTPServer
import CGIHTTPServer
from mimetypes import MimeTypes
import urllib 

class handler(CGIHTTPServer.CGIHTTPRequestHandler):  
    def is_cgi(self):
        mime = MimeTypes()
        request = self.path.split('?')
        if len(request) == 2:
            path, args = request
        else:
            path, args = request, None

        if isinstance(path, list):
            path = path[0]

        url = urllib.pathname2url(path)
        mime_type = mime.guess_type(url)

        if 'python' in mime_type[0]:
            self.cgi_info = '', self.path[1:]
            return True
        else:
            self.cgi_info = '', '/static.py?path=%s' % path[1:]
            print self.cgi_info
            return True

server = BaseHTTPServer.HTTPServer
server_address = ("", 8000)
handler.cgi_directories = ["/somedir/..."]

httpd = server(server_address, handler)
httpd.serve_forever()

静态.py:

#!/usr/bin/python2

import cgi
import urllib
from mimetypes import MimeTypes

form = cgi.FieldStorage()
mime = MimeTypes()

path = form.getvalue('path')

url = urllib.pathname2url(path)
mime_type = mime.guess_type(url)

print """Content-type: %s""" % mime
print 
print open(path, 'r').read()
于 2017-01-08T12:29:22.690 回答