python之Web开发

web框架:

python之Web开发

python之Web开发

这个很繁琐,心态容易崩。


flask安装:指定阿里云进行安装

python之Web开发

python之Web开发

python之Web开发

python之Web开发

具体源码:

pycharm中:ALT+ENTER是个好用法

from flask import Flask

app = Flask(__name__)


@app.route('/hello_world', methods=['GET', 'POST'])
def hello_world():
    from flask import request
#两种不同的提交方式判断
    if request.method == 'GET':
#可以构造XSS攻击脚本
#如:%3cscript%3ealert%28%27hehe%27%29%3c%2fscript%3e
#对应:<script>alert('hehe')</script>
#一般浏览器会检测到,并提示:错误代码 ERR_BLOCKED_BY_XSS_AUDITOR
        name = request.args.get('name')
        pwd = request.args.get('pwd')
        return '''
        <form action="/hello_world" method="post">
        <input name="name" placeholder="用户名"><br>
        <input name="pwd" placeholder="密码"><br>
        <button>确认</button>    
        </form>    
        <p>用户名:%s</p>
        <p>密码:%s</p>
        ''' % (name, pwd)
    else:
        name = request.form.get('name')
        pwd = request.form.get('pwd')
        return '''
        <script>alert('hehe')</script>
        <p>用户名:%s</p>
        <p>密码:%s</p>
        ''' % (name, pwd)

获取表单数据也可以这么写:

python之Web开发
if __name__ == '__main__':
    app.run()