@app.route('/restruct', methods=['POST']) def restruct(): dch = request.form['dch'] dch_folder = "restr" + dch count = 1 while os.path.exists(os.path.join('D:\cj', dch_folder)): dch_folder = "restr" + dch + "_" + str(count) count += 1 os.makedirs(os.path.join('D:\cj', dch_folder, 'photos')) os.makedirs(os.path.join('D:\cj', dch_folder, 'kmls')) os.makedirs(os.path.join('D:\cj', dch_folder, 'project')) photos_folder = os.path.join('D:\cj', dch_folder, 'photos') kmls_folder = os.path.join('D:\cj', dch_folder, 'kmls') project_folder = os.path.join('D:\cj', dch_folder, 'project') for file in request.files.getlist('files'): filename = file.filename file.save(os.path.join(photos_folder, filename)) dhs = request.form.getlist('dh') for dh in dhs: src_path = os.path.join(r'D:/911kml/', dh + '.xlsx.kml') dst_path = os.path.join(r'D:/cj', dch_folder, 'kmls/', dh + '.xlsx.kml') shutil.copy(src_path, dst_path) subprocess.Popen(["python", "restruct.py", dch, photos_folder, project_folder, kmls_folder])使用python实现,这段程序启动restruct.py后,restruct.py每生成一次信息,都实时发送给前端接收
时间: 2023-06-24 09:08:33 浏览: 170
这段代码的作用是创建一个名为"dch_folder"的文件夹,并在该文件夹中创建三个子文件夹photos、kmls、project。然后将用户上传的文件和指定文件夹中的文件复制到相应的子文件夹中。最后,使用subprocess模块启动restruct.py脚本,并将dch、photos_folder、project_folder、kmls_folder这四个参数传递给该脚本。restruct.py脚本会实时生成信息并发送给前端接收。
相关问题
from flask import Flask, request, jsonify import os app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload(): dch = request.form.get('dch') folder_name = f"{dch}duichang" if not os.path.exists(folder_name): os.mkdir(folder_name) for file in request.files.getlist('files'): filename = file.filename file.save(os.path.join(folder_name, filename)) return jsonify({"code": 200, "msg": "success"}) if __name__ == '__main__': app.run(host='192.168.1.226', port=7000)这段代码运行后run窗口会输出C:\Users\a212\AppData\Local\Programs\Python\Python36\python.exe D:\gangko\t1.py * Serving Flask app 't1' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://192.168.1.226:7000/ (Press CTRL+C to quit),如何更改可以运行后没有WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.这段警告
可以在运行 Flask 应用程序时设置环境变量 `FLASK_ENV` 的值为 `production`,以便启用生产模式。修改代码如下:
```python
import os
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/upload', methods=['POST'])
def upload():
dch = request.form.get('dch')
folder_name = f"{dch}duichang"
if not os.path.exists(folder_name):
os.mkdir(folder_name)
for file in request.files.getlist('files'):
filename = file.filename
file.save(os.path.join(folder_name, filename))
return jsonify({"code": 200, "msg": "success"})
if __name__ == '__main__':
app.config['ENV'] = 'production'
app.run(host='192.168.1.226', port=7000)
```
这样运行代码后,就不会再输出 `WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.` 这段警告信息了。
from flask import Flask, request, jsonify import os app = Flask(name) @app.route('/upload', methods=['POST']) def upload(): dch = request.form.get('dch') folder_name = f"{dch}duichang" if not os.path.exists(folder_name): os.mkdir(folder_name) for file in request.files.getlist('files'): filename = file.filename file.save(os.path.join(folder_name, filename)) return jsonify({"code": 200, "msg": "success"}) if name == 'main': app.run(host='192.168.1.226', port=7000)这段代码运行后run窗口会输出C:\Users\a212\AppData\Local\Programs\Python\Python36\python.exe D:\gangko\t1.py * Serving Flask app 't1' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://192.168.1.226:7000/ (Press CTRL+C to quit),如何更改可以运行后没有WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.这段警告
可以通过设置 `FLASK_ENV` 环境变量的值为 `production` 来禁用开发模式,从而避免警告信息的输出。在 Windows 环境下,可以在命令行中输入以下命令:
```
set FLASK_ENV=production
python t1.py
```
其中 `t1.py` 是你的 Flask 应用程序所在的 Python 文件名。这样运行 Flask 应用程序就不会有开发模式的警告了。
阅读全文