【问题标题】:How to execute python script on the BaseHTTPSERVER created by python?如何在python创建的BaseHTTPSERVER上执行python脚本?
【发布时间】:2012-10-15 10:37:48
【问题描述】:

我已经简单地创建了一个 python 服务器:

python -m SimpleHTTPServer

我有一个.htaccess(我不知道它是否对python服务器有用) 与:

AddHandler cgi-script .py
Options +ExecCGI

现在我正在编写一个简单的 python 脚本:

#!/usr/bin/python
import cgitb
cgitb.enable()
print 'Content-type: text/html'
print '''
<html>
     <head>
          <title>My website</title>
     </head>
     <body>
          <p>Here I am</p>
     </body>
</html>
'''

我将 test.py(我的脚本的名称)作为一个执行文件:

chmod +x test.py

我用这个地址在 Firefox 中启动:(http : //) 0.0.0.0:8000/test.py

问题,脚本没有执行...我在网页中看到代码... 服务器错误是:

localhost - - [25/Oct/2012 10:47:12] "GET / HTTP/1.1" 200 -
localhost - - [25/Oct/2012 10:47:13] code 404, message File not found
localhost - - [25/Oct/2012 10:47:13] "GET /favicon.ico HTTP/1.1" 404 -

如何简单地管理 python 代码的执行?是否可以在 python 服务器中编写来执行 python 脚本,就像这样:

import BaseHTTPServer
import CGIHTTPServer
httpd = BaseHTTPServer.HTTPServer(\
    ('localhost', 8123), \
CGIHTTPServer.CGIHTTPRequestHandler)
###  here some code to say, hey please execute python script on the webserver... ;-)
httpd.serve_forever()

或者别的什么...

【问题讨论】:

    标签: python http webserver cgi


    【解决方案1】:

    您可以使用更简单的方法并使用--cgi 选项启动python3 版本的http 服务器:

    python3 -m http.server --cgi
    

    如命令所指:

    python3 -m http.server --help
    

    【讨论】:

      【解决方案2】:

      您是否尝试过使用Flask?这是一个轻量级的服务器库,让这一切变得非常简单。

      from flask import Flask
      
      app = Flask(__name__)
      
      
      @app.route('/')
      def hello_world():
          return '<title>Hello World</title>'
      
      
      if __name__ == '__main__':
          app.run(debug=True)
      

      返回值,在本例中为 &lt;title&gt;Hello World&lt;/title&gt;,呈现为 HTML。您还可以将 HTML 模板文件用于更复杂的页面。

      这是一个简短的 youtube tutorial,可以更好地解释它。

      【讨论】:

        【解决方案3】:

        CGIHTTPRequestHandler 是正确的,因为.htaccess 文件对内置的 http 服务器没有任何意义。有一个CGIHTTPRequestHandler.cgi_directories 变量指定可执行文件被视为cgi 脚本的目录(here is the check itself)。您应该考虑将test.py 移动到cgi-binhtbin 目录并使用以下脚本:

        cgiserver.py:

        #!/usr/bin/env python3
        
        from http.server import CGIHTTPRequestHandler, HTTPServer
        
        handler = CGIHTTPRequestHandler
        handler.cgi_directories = ['/cgi-bin', '/htbin']  # this is the default
        server = HTTPServer(('localhost', 8123), handler)
        server.serve_forever()
        

        cgi-bin/test.py:

        #!/usr/bin/env python3
        print('Content-type: text/html\n')
        print('<title>Hello World</title>')
        

        你应该得到:

        |- cgiserver.py
        |- cgi-bin/
           ` test.py
        

        使用python3 cgiserver.py 运行并向localhost:8123/cgi-bin/test.py 发送请求。干杯。

        【讨论】:

        • 感谢您的回答!我将第一行替换为: from BaseHTTPServer import HTTPServer from CGIHTTPServer import CGIHTTPRequestHandler 但我不明白你为什么说你应该考虑将 test.py 移动到 cgi-bin 或 htbin 这些目录在哪里?是否可以生成它(有或没有root权限?)
        • 如果您修改cgi_directories 列表,则无需将test.py 放入cgi-bin 目录。将其设置为handler.cgi_directories = ['/'] 将允许您在同一目录中拥有test.pycgiserver.py。否则cgi-binhtbin 只是相对于cgiserver.py 的两个目录。我会更新答案。
        • 是的,它的工作!太感谢了!!!我发现没有任何网站或论坛解释得像你解释的那样清楚!!谢谢!
        • @gvalkov 我能以某种方式从http://localhost:8123/ 提供python 脚本吗?我尝试设置cgi_directories = ['/'] 并将我的python 放入index.html,但它抱怨code 403, message CGI script is not a plain file ('//')。附:我很抱歉挖出旧答案。 :(
        • 你好@CyprianGuerra。如果您坚持对根资源使用 CGI 脚本,则必须覆盖 CGIHTTPRequestHandleris_cgi 方法并设置为 self.cgi_info = '/', 'your-script.py'; return True。如果您不想处理 cgi,您也可以考虑只实现 SimpleHTTPRequestHandler 子类的 do_GET 方法。
        猜你喜欢
        • 2013-12-27
        • 1970-01-01
        • 2019-06-10
        • 2016-08-02
        • 2016-04-08
        • 1970-01-01
        • 2013-07-02
        • 2021-10-02
        • 2017-09-03
        相关资源
        最近更新 更多