7.11-Python-语言及其应用-笔记-剖析Web--未完

xiaoxiao2021-02-28  59

三个概念 Http (超文本传输协议),HTML(超文本标记语言),URL(资源定位符)

客户端: 最底层的数据传输协议:Tcp\IP 协议,传输的是字节,客户端创建的是Tcp\Ip协议,然后通过Http协议发送url和其他信息,返回一个响应,响应的格式也是由http进行定义的 “无状态”: 每一个请求都是相互独立的,这个本身是可以简化web操作的,但是同时也把其他操作变得复杂,比如下面的三种操作: 1.缓存,远程没有改变的东西保存在web 客户端

session() ,网站必须记住购物车里面的东西认证,使用密码登陆以后可以记录 登陆状态

3.使用telnet 进行测试 连接google 的80端口命令

$telnet www.google.com 80

两种http命令 ,一种是get,一种是head命令 head/HTTP/1.1 GET 命令得到内容更加详细,包括头部+首页的信息


9.1.2 python 标准库,两种 1. http 和urllib 两个库 ,urllib是http的高级层,下面是使用标准库获得网站的内容,urllib里面的方法urlopen(),read() 的方法,其中conn 里面还有其他重要的信息,比如其中的状态码code status

import urllib.request as b url = 'http.....' conn = b.urlopen(url) data = conn.read() print data

2 状态码类型分为以下的几种 : 1xx,服务器端收到数据,但是还需要客户端传送别的数据 2XX,成功 3XX,资源位已经改变,响应会重新给一个url,给客户端 4XX,客户端的问题 5XX,服务器的问题,502 表示网关的问题?? 后台应用服务器和web服务器程序


9.1.3 python 非标准库requests 比标准库更加简单和高效,代码量更加小

import requests url = "www.baidu.com" resp = requests.get(url) print (resp.text)

9.2 web服务器端 1. 启动最简单的服务器$python -m http.server 2. 浏览器输入相对路径127.0.0.1 - - [时间] "GET/HTTP/1.1" ,127.0.0.1 表示的是本机,第一个- 表示的是 远程用户,第二个用户表示的是- 登陆用户 ,GET 表示请求方式,/HTTP/表示请求的资源


9.3 Web 服务器网关接口 (WSGI) 通用网关(CGI): 由服务器获得用户输入的参数,然后传给CGI,再由CGI 传给外部程序,这样的缺点在于每一次要执行整个流程,很难扩大用户规模,启动的时候还需要等待 解决的方式: 把动态语言写在web服务器中去,比如Apache里面有很多模块可以运行不同的动态语言 web服务器网关接口(WSGI):是一个通用的API,用来连接python应用(python框架)和web服务器


9.2.4 Bottle 框架 1.@route 装饰器修饰功能是把 home()函数 和 对应的url对应起来,在

from bottle import route,run @route('https://sam-for-test-leon-ghibli-1.c9users.io') def home(): return 'Hello world!' run(host='localhost',port=8080) 2. 使用static_file,表示使用的模板的形式把HTML文档和代码进行分开,`root='.' ` 表示root目录是当前的目录 @route('/') def main(): return static_file('index.html',root='.') run(host='locahost',port='9999') 通过传参的形式输入到static_file文档中去 @route('https://sam-for-test-leon-ghibli-1.c9users.io/echo/<thing>') def echo(thing): return 'Say hello to Sam: %s! '%thing run(host='localhost',port='8080')

Flask框架 1. flask 默认的静态文档的目录是static,路径名称是/static开始的 ,如果有需要可以进行修改,static_folder='.' 表示当前目录,static_url_path='' 修改路径,原本的路径前面自动加上/static,@app.route 表示 浏览器后面和函数进行关联

from flask import Flask app = Flask(__name__,static_folder='.',static_url_path='') @app.route('/') def home(): return app.send_static_file('index.html') @app.route('/echo/<thing>') def echo(thing): return "say %s to Sam" %thing app.run(port=8080,debug=true)

Flask 比bottle 更加优秀的地方,存在一个jinja2模块,是一个具有扩展性的模板系统,

2.1使用{{ thing }}的方式渲染template模板 from flask import Flask,render_template app=Flask(__name__) @app.route('/echo/<thing>') def echo(): return render_template('flask2.html',thing =thing) app.run(port=999,debug=True) Template: {{ thing }} url:http://localhost:9999/echo/Rodan/McKeeport 2.2 通过url带参数的形式,把参数带入模板中 from flask import Flask,render_template app = Flask(__name__) @route('/echo/<thing>/<place>') def echo(): return render_template('flask3.html',thing = thing,place =place) 2.3 url带参数,通过get的方式取值,url的写法需要注意 @app.route('/echo/') def echo(): thing = request.args.get("thing") place = request.args.get("place") return render_template('flask3.html',thing =thing,place =place) app.run(port=9999,debug=True) url:?的方式传递 2.4 简化2.3,使用**操作符一次向模板传入字典的多个数据,这个其实和我现在做的差不多只是,我在传输的时候使用{}的方式 from flask import Flask,render_template,request app=flask(__name__) @app.route('/echo/') def echo(): kwargs = {} kwargs['thing'] = request.args.get('thing') kwargs['place'] = request.args.get('place') return render_template('flask3.html',**kwargs)
转载请注明原文地址: https://www.6miu.com/read-42207.html

最新回复(0)