最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[记录]CentOS中继续研究Flask服务器

CentOS crifan 2789浏览 0评论

试试:

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/

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)

找到中文文档:

欢迎使用 Flask — Flask 0.10.1 文档

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

结果出错:

NameError

NameError: 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_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

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:
  • dump() shows all variables in the frame
  • dump(obj) dumps all that’s known about the object
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

再去:

[记录]折腾Flask服务器的flash功能

转载请注明:在路上 » [记录]CentOS中继续研究Flask服务器

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
81 queries in 0.181 seconds, using 22.09MB memory