Python3.5开发7 - Web开发
7-1 Flask简介与安装
Flask是一个使用Python编写的轻量级Web应用框架。基于Werkzeug WSGI工具箱和Jinja2 模板引擎。
- 内置开发用服务器和debugger
- 集成单元测试(unit testing)
- RESTful request dispatching
- 使用Jinja2模板引擎
- 支持secure cookies(client side sessions)
- 100% WSGI 1.0兼容
- Unicode based
- 详细的文件、教学
- Google App Engine兼容
- 可用Extensions增加其他功能
安装:
pip install flask #这是flask框架的主体
pip install flask-wtf #这个库位flask增加了表单的功能
pip install flask-bootstrap #这个库为flask增加了使用bootstrap的能力
7-2 第一个网页
代码:
from flask import Flask, url_for, redirect
app = Flask(__name__)
@app.route('/')
def hello_world():
a = a + 1
return '我是首页!'
# @app.route('/second')
# def second():
# a = '''
#
# 超级长的源代码
# '''
# return a
@app.route('/test/<name>')
def test(name):
b = int(name) * 100
return '你好, {}'.format(b)
@app.route('/toindex')
@app.route('/toindex/<command>')
def toindex(command=''):
if command == 'toindex':
return url_for('toindex', command=123)
else:
return '没有找到'
@app.route('/toRedirect')
def toRedict():
return redirect('/')
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug=True) # 可以接受外界任何电脑的访问
PIN码
7-3 网页模板
Jinja2的语法:
变量:
{{ 变量名 }}
条件判断:
{% if xxx %}
内容
{% elif xxx %}
内容2
{% endif %}
For循环:
{% for xxx in yyy %}
内容
{% endfor %}
7-4 表单
如何查看表单密码:将type="password"改为type=“text”,可以显示密码
7-5 面向对象编程
- 类的定义
- init(self)
- 属性
- 方法
- 继承
网页端代码分析
杀被控端代码分析
本次课程中我们学习了基于Python的Web开发,你应当掌握如下知识:
- 使用Flask框架开发简单的网页
- 理解面向对象编程
- 能读懂老师提供的远程控制程序的代码