1

この質問 ( Python CGIHTTPServer Default Directories ) では、Python CGIHTTPServer の 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、2つの要素を追加します

  • CGIHTTPServer.CGIHTTPServer._url_collapse_path(self.path)ファイルの外で使用するにはCGIHTTPServer.py
  • さらに重要: 拡張機能のチェック

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

    しかし、それはもっとうまくいく可能性があります。

使いませんでした

    if tail.endswith('.py'):

サーバーは、必要に応じて他の言語でスクリプトを実行する可能性があるためです。PerlPHPBashなど

コード:

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 または静的ファイル) を検出する必要があります。Mimetypeが役立つ場合があります。静的ファイルが要求された場合、静的ファイルを配信する別の cgi スクリプトを呼び出すことができます。ところで。古い cgi の代わりに wsgi を使用する必要があります。

私が入手したいくつかの古いコード (py2.7) を変更しました。これは非常に見苦しく、一度も使用したことがありません。

サーバー.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()

static.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 に答える