我目前正在使用python3和Flask; 我使用相同的路由定义了两个函数。-如何index2打印。
from flask import Flask, request, make_response
app = Flask(__name__)
@app.route('/')
def index():
if request.authorization and request.authorization.username == 'user1' and request.authorization.password == 'pass1':
return '<h1>You are logged in</h1>'
return make_response('Could not verify!', 401, {'WWW-Authenticate' : 'Basic realm="Login Required"'})
@app.route('/')
def index2():
print('In Index 2')
if __name__ == '__main__':
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud) 假设我有一个服务器代码,当调用函数 savePersonList() 时,控制台上打印的结果应保存到文本文件中。
#server coding
app = Flask(__name__)
def allowed_file(filename):
# this has changed from the original example because the original did not work for me
return filename[-3:].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST']) #integrate code below function
def upload_file():
if request.authorization and request.authorization.username == 'user1' and request.authorization.password == 'pass2': #login code line
if request.method == 'POST':
print("This is a post Request \n\n")
file = request.files['file']
if file and allowed_file(file.filename):
print("File Sent is valid \n\n")
print('**found file', file.filename)
filename = secure_filename(file.filename) …Run Code Online (Sandbox Code Playgroud)