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.这段警告
时间: 2024-03-28 21:42:23 浏览: 124
可以通过设置 `FLASK_ENV` 环境变量的值为 `production` 来禁用开发模式,从而避免警告信息的输出。在 Windows 环境下,可以在命令行中输入以下命令:
```
set FLASK_ENV=production
python t1.py
```
其中 `t1.py` 是你的 Flask 应用程序所在的 Python 文件名。这样运行 Flask 应用程序就不会有开发模式的警告了。
相关问题
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.` 这段警告信息了。
我在pythonanywhere上添加这些文件,那么系统默认的那几个文件哪些可以删除,哪些要保留:from flask import Flask, render_template, request, redirect, url_for from werkzeug.utils import secure_filename from models import db, File app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///files.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SECRET_KEY'] = 'secret_key' app.config['UPLOAD_FOLDER'] = 'uploads' db.init_app(app) @app.route('/') def index(): return render_template('upload.html') @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] filename = secure_filename(file.filename) file.save(app.config['UPLOAD_FOLDER'] + '/' + filename) f = File(name=filename) db.session.add(f) db.session.commit() return redirect(url_for('index')) if __name__ == '__main__': app.run()
在PythonAnywhere上,您可以删除以下文件:
1. requirements.txt (如果您没有使用外部库)
2. 任何包含敏感信息的文件,比如密码和私钥,这些文件不应该被上传到PythonAnywhere上。
请注意,如果您使用PythonAnywhere上的Web应用程序,则必须保留wsgi.py文件。在这种情况下,您的应用程序应该在wsgi.py中被导入。如果您使用Flask,则应该将您的Flask应用程序对象保存在wsgi.py中的一个变量中,并在文件中进行导入。
阅读全文