网站开发的工作制度,中国工程建设造价信息网站,一流的南昌网站建设,PC网站开发的意义最近准备整理一下之前学过的前端小程序知识笔记#xff0c;形成合集。顺便准备学一学接口部分#xff0c;希望自己能成为一个全栈嘿嘿。建议关注收藏#xff0c;持续更新技术文档。 目录 前端知识技能树http请求浏览器缓存 后端知识技能树python_api#xff1a;flaskflask…最近准备整理一下之前学过的前端小程序知识笔记形成合集。顺便准备学一学接口部分希望自己能成为一个全栈嘿嘿。建议关注收藏持续更新技术文档。 目录 前端知识技能树http请求浏览器缓存 后端知识技能树python_apiflaskflask_restfulpython_apiFastAPITornado 前端知识技能树
http请求
GET和POST是HTTP请求的两种基本方法
GETPOSTGET把参数包含在URL中POST通过request body传递参数GET在浏览器回退时是无害的POST会再次提交请求GET请求会被浏览器主动cachePOST不会除非手动设置GET产生的URL地址可以被BookmarkPOST不可以GET请求只能进行url编码POST支持多种编码方式GET比POST更不安全因为参数直接暴露在URL上所以不能用来传递敏感信息。GET请求参数会被完整保留在浏览器历史记录里POST中的参数不会被保留。GET请求在URL中传送的参数是有长度限制的没有对参数的数据类型GET只接受ASCII字符没有限制GET产生一个TCP数据对于GET方式的请求浏览器会把http header和data一并发送出去服务器响应200返回数据POST产生两个TCP数据包对于POST浏览器先发送header服务器响应100 continue浏览器再发送data服务器响应200 ok返回数据。
HTTP有GET, POST, PUT, DELETE等等HTTP的底层是TCP/IP。所以GET和POST的底层也是TCP/IP
大多数浏览器通常都会限制url长度在2K个字节而大多数服务器最多处理64K大小的url超过的部分恕不处理。
因为POST需要两步时间上消耗的要多一点看起来GET比POST更有效。但是
GET与POST都有自己的语义不能随便混用。据研究在网络环境好的情况下发一次包的时间和发两次包的时间差别基本可以无视。而在网络环境差的情况下两次包的TCP在验证数据包完整性上有非常大的优点。并不是所有浏览器都会在POST中发送两次包Firefox就只发送一次。
浏览器缓存
后端知识技能树
python_apiflaskflask_restful
在开始之前呢我们需要安装几个模块flaskflask_restful
get接口
# codingutf-8
import sys
import importlib
importlib.reload(sys)
from flask import *
import flask_restfulapp Flask(__name__)
api flask_restful.Api(app)class HelloWorld(flask_restful.Resource):def get(self):xrequest.args[x]#获取参数中的值yrequest.args[y]return {hello:y,donghu:x}#接口返回值api.add_resource(HelloWorld, /login,methods[GET])#页面路径if __name__ __main__:app.run(host0.0.0.0,port80)#请求地址以及端口
get接口编写完成运行然后在浏览器中输入http://127.0.0.1/login 能正常返回值那就说明没有问题了。
post接口
# codingutf-8
import sys
import importlib
importlib.reload(sys)
from flask import *
import flask_restfulapp Flask(__name__)
api flask_restful.Api(app)class HelloWorld(flask_restful.Resource):def post(self):x request.form[x]#获取参数yrequest.form[y]return {hello:y,donghu:x}api.add_resource(HelloWorld, /login2,methods[POST])if __name__ __main__:app.run(host0.0.0.0,port80)post接口和get接口编写方式上差不多只是接收参数的方式稍有调整。运行然后在浏览器中输入http://127.0.0.1/login2,看是否能正常访问。
python_apiFastAPITornado
python提供了很多web框架帮助我们快速构建API如Flask、FastAPI、Tornado。 Flask、FastAPI如出一辙所以这里只讲FastAPITornado如何构建GET和POST接口。
pip install fastapi pip install uvicorn pip install tornado
# -*- coding: utf-8 -*-
from fastapi import FastAPI
from pydantic import BaseModel
import uvicornapp FastAPI()
class Item(BaseModel):a: int Noneb: int Noneapp.get(/test/a{a}/b{b})
def calculate(a: intNone, b: intNone):c a bres {res:c}return resapp.post(/test)
def calculate(request_data: Item):a request_data.ab request_data.bc a bres {res:c}return res if __name__ __main__:uvicorn.run(appapp,hostlocalhost,port8000,workers1)将上述代码保存为get.py,存储在某一路径 首先进入python文件路径在控制台启动服务 浏览器测试访问http://localhost:8000/test/a31/b12
postman测试 FastAPI的交互测试访问http://localhost:8000/docs Tornado比FastAPI更能承受高并发。
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado import gen
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor
import time
from tornado.options import define, options
from tornado.platform.asyncio import to_asyncio_future,AsyncIOMainLoop
from tornado.httpclient import AsyncHTTPClient
import asynciodefine(port, default8000, helpjust run, typeint)class MainHandler(tornado.web.RequestHandler):def main(self,a,b):c float(a) float(b)res {res:c}return res# get接口 def get(self):a self.get_body_argument(a)b self.get_body_argument(b)res self.main(a,b) # 主程序计算self.write(json.dumps(res,ensure_asciiFalse))# post接口 def post(self):a self.get_body_argument(a)b self.get_body_argument(b)res int(a) * int(b) #主程序计算self.write(json.dumps(res,ensure_asciiFalse))if __name__ __main__:#运行程序application tornado.web.Application([(r/test, MainHandler),])application.listen(8000,localhost)tornado.ioloop.IOLoop.instance().start()