如何在python创建的BaseHTTPSERVER上执行python脚本? [英] How to execute python script on the BaseHTTPSERVER created by python?

查看:285
本文介绍了如何在python创建的BaseHTTPSERVER上执行python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是创建了一个python服务器:

I have simply created a python server with :

python -m SimpleHTTPServer

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

I had a .htaccess (I don't know if it is usefull with python server) with:

AddHandler cgi-script .py
Options +ExecCGI

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

Now I am writing a simple python script :

#!/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(我的脚本名称)成为执行文件with:

I make test.py (name of my script) an executed file with:

chmod +x test.py

我在firefox中使用此地址启动:(http://)0.0.0.0:8000/test.py

I am launching in firefox with this addres: (http : //) 0.0.0.0:8000/test.py

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

Problem, the script is not executed... I see the code in the web page... And server error is:

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脚本,就像这样:

How can I manage the execution of python code simply? Is it possible to write in a python server to execute the python script like with something like that:

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()

或者别的...

推荐答案

你走在正确的轨道上 CGIHTTPRequestHandler ,因为 .htaccess 文件对内置的http服务器没有任何意义。有一个 CGIHTTPRequestHandler.cgi_directories 变量,它指定可执行文件被认为是cgi脚本的目录(这里是检查本身)。您应该考虑将 test.py 移至 cgi-bin htbin 目录并使用以下脚本:

You are on the right track with CGIHTTPRequestHandler, as .htaccess files mean nothing to the the built-in http server. There is a CGIHTTPRequestHandler.cgi_directories variable that specifies the directories under which an executable file is considered a cgi script (here is the check itself). You should consider moving test.py to a cgi-bin or htbin directory and use the following script:

cgiserver.py:

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:

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 并将请求发送到本地主机:8123 /的cgi-bin / test.py 。干杯。

Run with python3 cgiserver.py and send requests to localhost:8123/cgi-bin/test.py. Cheers.

这篇关于如何在python创建的BaseHTTPSERVER上执行python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆