Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python 3x-web服务器-脚本未执行_Python 3.x - Fatal编程技术网

Python 3.x Python 3x-web服务器-脚本未执行

Python 3.x Python 3x-web服务器-脚本未执行,python-3.x,Python 3.x,这是我的python 3.5 web服务器: #!/usr/bin/env python35 PORT = 80 import http.server httpd = http.server.HTTPServer( ("", PORT),http.server.CGIHTTPRequestHandler) httpd.cgi_directories = ["/"] httpd.serve_forever() 问题是:文件没有执行。web浏览器只输出文件的内容 它缺少了一些东西。 我不太擅长py

这是我的python 3.5 web服务器:

#!/usr/bin/env python35
PORT = 80
import http.server
httpd = http.server.HTTPServer( ("", PORT),http.server.CGIHTTPRequestHandler)
httpd.cgi_directories = ["/"]
httpd.serve_forever()
问题是:文件没有执行。web浏览器只输出文件的内容

它缺少了一些东西。
我不太擅长python。

只需使用它来创建一个完全由bash脚本组成的网站。

这并不是那么简单,可能需要几天的时间来完成完整的应用程序。我将给出一个小的蓝图,您可以将其作为使用python和进行进一步开发的基础

为了使应用程序保持简单,我假设shell脚本与应用程序保存在同一目录中。要构建应用程序,您可以使用flask framework,这是一个微框架,它使得在python中构建简单的应用程序非常容易。Python有一个
子流程
模块,可以调用它来执行后面的shell脚本。根url将与指向文件的链接一起提供目录中的文件,如果单击文件,则用户将被路由到另一个url,该url将具有所单击的bash脚本的输出

因此,您的项目必须有两个文件。python文件(我们可以称之为my_server.py)和模板文件:home.html保存在templates文件夹中,用于处理根文件夹中不同文件的显示。因此,您的根文件夹如下所示。sh是一个示例脚本文件,保存在那里用于测试。您将在那里拥有自己的脚本文件

drwxrwxr-x templates
    -rwxr--r-- home.html
-rw-rw-r-- script.sh
-rwxr--r-- my_server.py
home.html的内容:

<html>
<body>
  <ul>
    {% for i in data: %}
      <li><a href="{{ url_for('script_file', filename=i)}}">
        Files: {{ i }}
      </a></li>
    {% endfor %}
  </ul>
</body>
</html>

    {数据中的i为%1:%}
  • {%endfor%}
my_server.py的内容

import os
import subprocess

from flask import Flask
from flask import render_template
app = Flask(__name__)


@app.route('/')
def get_data():
    def file_in_dir():
        for entry in make_tree(get_root_dir()):
            yield entry
    return render_template('home.html', data=file_in_dir())


@app.route('/<filename>')
def script_file(filename):
    if ".sh" in filename:
        shell_script =  {'file_path': get_root_dir(), 'filename': filename}
        subprocess.call("bash {file_path}/{filename} > out.txt".format(**shell_script), shell=True)
        with open("out.txt") as f:
            contents = f.read()
        return contents
    else:
        return "Not an executable bash script"


def make_tree(path):
    lst = "No files"
    try: lst = os.listdir(path)
    except OSError:
        pass #ignore errors
    return lst


def get_root_dir():
    return os.path.dirname(os.path.realpath(__file__))


if __name__ == "__main__":
    app.run()
导入操作系统
导入子流程
从烧瓶进口烧瓶
从flask导入渲染模板
app=烧瓶(名称)
@应用程序路径(“/”)
def get_data():
def file_in_dir():
对于make_树中的条目(get_root_dir()):
收益率条目
返回render_模板('home.html',data=file_in_dir())
@应用程序路径(“/”)
def脚本_文件(文件名):
如果文件名中有“.sh”:
shell_脚本={'file_path':get_root_dir(),'filename':filename}
subprocess.call(“bash{file\u path}/{filename}>out.txt”。格式(**shell\u脚本),shell=True)
以open(“out.txt”)作为f:
contents=f.read()
返回内容
其他:
返回“不是可执行的bash脚本”
def生成树(路径):
lst=“无文件”
try:lst=os.listdir(路径)
除操作错误外:
通过#忽略错误
返回lst
def get_root_dir():
返回os.path.dirname(os.path.realpath(_文件__))
如果名称=“\uuuuu main\uuuuuuuu”:
app.run()

我希望这足以让您开始。

我自己在那里添加了
httpd.cgi_目录=[“/”]
。这可能是有缺陷的。你能安装像flask这样的第三方库吗?@joydeep bhattacharjee,我想要简单,我甚至不想使用python。。但是我想创建一个由bash脚本组成的网站。