【问题标题】:Python Flask import listPython Flask 导入列表
【发布时间】:2017-08-04 00:22:17
【问题描述】:

我是 python 新手,似乎无法弄清楚..

我将以下代码保存为 content.py

    import fnmatch
import os
matches = []
for root, dirnames, filenames in os.walk("Z:\\"):
    for filename in fnmatch.filter(filenames, '*.iso'):
        matches.append(os.path.join(root, filename))

for item in matches:
    print(item)

我在 website.py 中有以下网站代码:

    from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run('192.168.1.8', 8080)

我尝试了许多组合以使其正常工作,因此我的匹配项以某种方式添加到 website.py 中。我可以将列表直接添加到 website.py 中吗?该列表将包含几行操作系统 iso 副本。

我想将 website.py 作为 cron 作业每小时运行一次。

我是否需要在 website.py 中停止网站以刷新 cron 作业中的内容?:

    @app.route('/shutdown', methods=['POST'])
def shutdown():
    shutdown_server()
    return 'Server shutting down...'

很抱歉这些问题,但我更像是一个脚本编写者.. 所以使用我知道的东西但使用程序语言是非常不同的。

谢谢, 病房

【问题讨论】:

  • 因为你需要一些数据来持久化,你可以使用像 Sqlite3 这样的数据库,或者只是让你的 cron 将结果转储到一个 json 文件中并在请求时读取该文件。但是,如果您要获得任何类型的流量,您将需要创建一个数据库,因为每次打开文件以读取它时,您都会阻止任何其他打开文件的请求。
  • 仅供个人使用。所以 json 文件可以工作。

标签: python list flask cron


【解决方案1】:

解决办法:

内容.py

    import fnmatch
import os
matches = []
for root, dirnames, filenames in os.walk("Z:\\"):
    for filename in fnmatch.filter(filenames, '*.iso'):
        matches.append(os.path.join(filename))

new_list = matches

result_string = """<HTML>
<body>
    <h1>Attendance list</h1>
    <table>\n"""
for i in new_list:
    result_string += "        <tr>\n            "
    for j in i:
        result_string += "<td>%s</td>" %j
    result_string += "\n        </tr>\n"
result_string += """    </table>
</body>
</HTML>"""
display = open("table.html", 'w')
display.write(result_string)
display.close()

##for item in matches:
    ##print(item)

网站.py

import flask

app = flask.Flask('flasksubs')
webcode = open('table.html').read()

@app.route('/')
def webprint():
    return webcode 

if __name__ == '__main__':
    app.run(host = '10.1.16.14', port = 82)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 2022-11-17
    • 2020-09-11
    • 2022-11-22
    • 2014-08-31
    • 1970-01-01
    相关资源
    最近更新 更多