试试:
from flask import Flask app = Flask(__name__) @app.route(‘/’) def index(): return ‘Index Page’ @app.route(‘/’) def hello(): return ‘Hello, World!’ |
效果:
http://115.29.173.126:5000/hello
->原来是自己不小心写错了
去改为:
from flask import Flask app = Flask(__name__) @app.route(‘/’) def index(): return ‘Index Page’ @app.route(‘/hello’) def hello(): return ‘Hello, World!’ |
即可:
继续去参考:
Quickstart — Flask Documentation (0.11)
找到中文文档:
https://www.ietf.org/rfc/rfc2068.txt
去加上:
@app.route(‘/login’, methods=[‘GET’, ‘POST’]) def login(): if request.method == ‘POST’: do_the_login() else: show_the_login_form() |
直接访问:
http://115.29.173.126:5000/login
结果出错:
NameErrorNameError: global name ‘request’ is not defined Traceback (most recent call last)File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__return self.wsgi_app(environ, start_response) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_appresponse = self.make_response(self.handle_exception(e)) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exceptionreraise(exc_type, exc_value, tb) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_appresponse = self.full_dispatch_request() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_requestrv = self.handle_user_exception(e) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exceptionreraise(exc_type, exc_value, tb) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_requestrv = self.dispatch_request() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_requestreturn self.view_functions[rule.endpoint](**req.view_args) File “/root/SIPEvents/hello.py”, line 24, in loginif request.method == ‘POST’:
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. To switch between the interactive traceback and the plaintext one, you can click on the “Traceback” headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side. You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
Brought to you by DON’T PANIC, your friendly Werkzeug powered traceback interpreter. |
log也输出:
(SIPEvents) SIPEvents flask run –host=0.0.0.0 * Serving Flask app “hello” * Forcing debug mode on * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger pin code: 353-795-063 106.187.92.207 – – [16/Aug/2016 14:48:23] “GET / HTTP/1.1” 200 – 106.187.92.207 – – [16/Aug/2016 14:48:30] “GET /login HTTP/1.1” 500 – Traceback (most recent call last): File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__ return self.wsgi_app(environ, start_response) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app response = self.make_response(self.handle_exception(e)) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception reraise(exc_type, exc_value, tb) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app response = self.full_dispatch_request() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request rv = self.handle_user_exception(e) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception reraise(exc_type, exc_value, tb) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request rv = self.dispatch_request() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File “/root/SIPEvents/hello.py”, line 24, in login if request.method == ‘POST’: NameError: global name ‘request’ is not defined 106.187.92.207 – – [16/Aug/2016 14:48:30] “GET /login?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1” 200 – 106.187.92.207 – – [16/Aug/2016 14:48:30] “GET /login?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1” 200 – 106.187.92.207 – – [16/Aug/2016 14:48:31] “GET /login?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1” 200 – 106.187.92.207 – – [16/Aug/2016 14:48:32] “GET /login?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1” 200 – 106.187.92.207 – – [16/Aug/2016 14:48:37] “GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1” 200 – 106.187.92.207 – – [16/Aug/2016 14:48:38] “GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1” 200 – |
from flask import render_template @app.route(‘/hello/’) @app.route(‘/hello/<name>’) def hello(name=None): return render_template(‘hello.html’, name=name) |
(SIPEvents) templates vi hello.html <!doctype html> <title>Hello from Flask</title> {% if name %} <h1>Hello {{ name }}!</h1> {% else %} <h1>Hello World!</h1> {% endif %} |
http://115.29.173.126:5000/hello/
http://115.29.173.126:5000/hello/Crifan
再去:
转载请注明:在路上 » [记录]CentOS中继续研究Flask服务器