编程语言
首页 > 编程语言> > Python Flask web入门例子

Python Flask web入门例子

作者:互联网

安装pip

-安装完python,需要手动设置pip环境变量,找到python安装目录的Scripts文件夹中就是根目录,比如C:\Python27\Scripts。设置完环境变量即可安装第三方模块了,安装时为了提高速度可以使用阿里镜像,方法如下

[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
 
[install]
trusted-host=mirrors.aliyun.com

安装Flask模块

pip install Flask

新建app.py文件,填入下面内容:

from flask import Flask
from flask import request

print __name__

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
    return '<h1>Home</h1>'

@app.route('/signin', methods=['GET'])
def signin_form():
    return '''<form action="/signin" method="post">
              <p><input name="username"></p>
              <p><input name="password" type="password"></p>
              <p><button type="submit">Sign In</button></p>
              </form>'''

@app.route('/signin', methods=['POST'])
def signin():
    if request.form['username']=='admin' and request.form['password']=='password':
        return '<h3>Hello, admin!</h3>'
    return '<h3>Bad username or password.</h3>'

if __name__ == '__main__':
    app.run()

运行

python app.py

打开浏览器访问http://127.0.0.1:5000/

标签:__,web,return,Python,app,signin,Flask,pip
来源: https://www.cnblogs.com/winfred/p/10371956.html