Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions content/backend/1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: "后端开发"
date: 2024-11-01
draft: false
---
## hduyw
Binary file added content/backend/FastAPI/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/backend/FastAPI/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions content/backend/FastAPI/Cookie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Cookie参数
```python
from typing import Annotated
from fastapi import Cookie,FastAPI
app=FastAPI()
@app.get("/item/")
async def read_items(ads_id:Annotated[str|None,Cookie()]=None):
return {"ads_id":ads_id}
```
声明Cookie参数

>async def read_items(ads_id:Annotated[str|None,Cookie()]=None):

## 参数模型
Cookie 的特性是 “浏览器自动携带、存储在本地”,数据存储在用户的浏览器里面,不能随意更改

```python
from typing import Annotated, Union

from fastapi import Cookie, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Cookies(BaseModel):
model_config = {"extra": "forbid"}

session_id: str
fatebook_tracker: Union[str, None] = None
googall_tracker: Union[str, None] = None


@app.get("/items/")
async def read_items(cookies: Annotated[Cookies, Cookie()]):
return cookies
```
这里的

>model_config = {"extra": "forbid"}

意思就是只接受指定的Cookie
53 changes: 53 additions & 0 deletions content/backend/FastAPI/Header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Header参数
```python
from typing import Annotated, Union

from fastapi import FastAPI, Header

app = FastAPI()


@app.get("/items/")
async def read_items(user_agent: Annotated[Union[str, None], Header()] = None):
return {"User-Agent": user_agent}
```
支持自动转换(大部分标准请求头用连字符分隔,即减号(-)。

但是 user-agent 这样的变量在 Python 中是无效的。)

支持接收重复请求头
```python
from typing import Annotated

from fastapi import FastAPI, Header

app = FastAPI()


@app.get("/items/")
async def read_items(x_token: Annotated[list[str] | None, Header()] = None):
return {"X-Token values": x_token}
```
## Header参数模型
```python
from typing import Annotated

from fastapi import FastAPI, Header
from pydantic import BaseModel

app = FastAPI()


class CommonHeaders(BaseModel):
host: str
save_data: bool
if_modified_since: str | None = None
traceparent: str | None = None
x_tag: list[str] = []


@app.get("/items/")
async def read_items(headers: Annotated[CommonHeaders, Header()]):
return headers
```
同样,与Cookie参数一样,禁止额外的Headers
126 changes: 126 additions & 0 deletions content/backend/FastAPI/basic_knowledge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Internet网络知识
## 互联网是如何工作的
1.互联网的本质与历史

本质:连接全球设备的巨型网络基础设施,核心作用是实现设备间稳定通信。

历史:起源于 1960 年代美国军方研究项目,1980 年代发展为公共基础设施,核心工作逻辑未变。

2.网络搭建的核心逻辑与设备

简单网络:两台设备可直接连接,多设备需通过 “路由器” 集中转发信息,减少连接复杂度。

规模化扩展:路由器之间可互相连接,实现网络范围无限扩大。

跨网络连接:通过 “调制解调器” 转换信号,借助电话 / 网线等现有基础设施,再通过 “ISP(互联网服务提供商)” 连接全球网络。

3.电脑的 “地址系统”

IP 地址:设备的唯一标识,由四段数字组成(如 192.0.2.172),供设备之间识别。

域名:IP 地址的可读别名(如google.com),解决人类记不住数字地址的问题。

4.互联网与 Web 的区别

互联网:底层基础设施,负责连接设备。

Web:建立在互联网上的服务,通过浏览器访问(如网站),此外邮箱、IRC 等也是互联网上的独立服务。

5.内联网与外联网的特点
内联网:组织专用的封闭网络,仅内部成员可访问,用于共享资源、协作。

外联网:开放部分权限给外部合作方的专用网络,用于安全共享信息、协作。

共性:均基于互联网基础设施和协议,授权成员可异地访问。
## 什么是互联网
互联网没有控制中心。相反,它是一种分布式网络系统;也就是说,它不依赖于任何单独的计算机。任何计算机或硬件只要能以正确方式(例如,使用正确的网络协议)发送和接收数据,都可成为互联网的一部分。
## HTTP概述
### 系统组成:

1.客户端(用户代理):
请求方

2.服务器:可以提供客户端想要的东西的独家或者服务器集群

3.代理:作用再客户端和服务器中间,例如:缓存,过滤,负载均衡
### 基本性质
1.简约好懂

2.可扩展

3.无状态,但有记忆效应

4.依赖TCP链接:要穿数据,首先需要建立TCP连接,再发出HTTP请求
### HTTP报文
#### 请求报文
1.动作(操作,例如POST,GET)
2.请求内容
3.协议版本
4.额外要求
5.附加信息
#### 响应报文
1.协议版本
2.状态码
3.状态说明
4.备注
5.实际资源
## 浏览器工作原理
导致Web性能的最大两个原因:1.网络延迟,2.浏览器的单线程执行

### DNS查询(第一次查询)

根据个人理解相当于在我们每次从电脑上输入网址去访问某个网站时,不能直接发HTTP请求,必须要把域名对应到ip地址才可以,dns服务器就是做将域名与ip地址匹配的工作的

### TCP握手

三次握手:让两端changshijinxingtongxin

### TLS协商
安全输出,互联网加密

对称加密:就类似于有一把钥匙可以打开和关闭房子,高效,但是安全性不够强,因为这把钥匙一旦被偷,整个家就不保了

非对称加密:公钥和私钥,公钥公开,大家可以来加密信息,但私钥只有一把在自己手上,用来打开家门查询数据,速度慢但安全

TLS结合了对称与非对称加密,传输通道采用非对称加密 ,数据加密采用对称机密
### 构建dom树
DOM 树描述了文档的内容。<html> 元素是第一个标签也是文档树的根节点。树反映了不同标记之间的关系和层次结构。嵌套在其他标记中的标记是子节点。DOM 节点的数量越多,构建 DOM 树所需的时间就越长。
### 预加载
提前缓存资源
### 构建CSSOM树
类似于dom树,针对css文件

## URL
http://www.example.com:80/path/to/myfile.html?key1=value&key2=value2#SomewhereInTheDocument

方案

>http

表示浏览器必须使用的协议来请求资源(协议是计算机网络中交换或传输数据的一组方法)

权威

>www.example.com:80

包括域(www.example.com)和端口(80)

如果 Web 服务器使用 HTTP 协议的标准端口(HTTP 为 80,HTTPS 为 443)来授予对其资源的访问权限,则通常会省略端口。否则,端口是强制的。

路径

>/path/to/myfile.html

资源路径

参数

>?key1=value&key2=value2

每个 Web 服务器都有自己关于参数的规则,唯一可靠的方式来知道特定 Web 服务器是否处理参数是通过询问 Web 服务器所有者。

## 域名
包括顶级域名和标签



84 changes: 84 additions & 0 deletions content/backend/FastAPI/class.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# 面向对象
```python
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score

def print_score(self):
print('%s: %s' % (self.name, self.score))
```
若想将这里代码中的name,score变成private,只需在其名称前加两个下划线
```python
class Student(object):
def __init__(self, name, score):
self.__name = name
self.__score = score

def print_score(self):
print('%s: %s' % (self.__name, self.__score))
```
在Python中,变量名类似__xxx__的,也就是以双下划线开头,并且以双下划线结尾的,是特殊变量,特殊变量是可以直接访问的,不是private变量,所以,不能用__name__、__score__这样的变量名。
```python
>>> bart = Student('Bart Simpson', 59)
>>> bart.get_name()
'Bart Simpson'
>>> bart.__name = 'New Name' # 设置__name变量!
>>> bart.__name
'New Name'
```
表面上看,外部代码“成功”地设置了__name变量,但实际上这个__name变量和class内部的__name变量不是一个变量!内部的__name变量已经被Python解释器自动改成了_Student__name,而外部代码给bart新增了一个__name变量。
# 继承和多态(类比c++)
```python
class Animal(object):
def run(self):
print('Animal is running...')
class Dog(Animal):
pass

class Cat(Animal):
pass
dog = Dog()
dog.run()

cat = Cat()
cat.run()
```
>Animal is running...
>
>Animal is running...

以上为继承,即子类可以继承父类的方法deng
```python
class Dog(Animal):
def run(self):
print('Dog is running...')

def eat(self):
print('Eating meat...')
```
>Dog is running...
>
>Cat is running...

当子类中的一些方法与父类重名,则会覆盖父类原来的方法,此为多态

有些需要注意的点是:
__xxx__的属性和方法在python帐都是有特殊用途的,比如__len__方法是返回长度
```python
>>>len('abc')
3
>>>'abc'.__len__()
3
```
如果自己创建的类,也想用len,需要自己写一个__len__()方法

hasattr(obj,'x')#判断obj对象中是否有x对象

setattr(obj,'y',19)#对obj对象设置一个新的属性,设初值为19

getattr(obj,'y')#获取属性‘y'

getattr(obj,'z',404)#获取属性'z',如果不存在,返回默认值404

isinstance(h,husky)判断h是否是husky类
Binary file added content/backend/FastAPI/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/backend/FastAPI/image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/backend/FastAPI/image-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/backend/FastAPI/image-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/backend/FastAPI/image-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/backend/FastAPI/image-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/backend/FastAPI/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions content/backend/FastAPI/path_params_numeric_validation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 路径参数和数值校验
引入Path(由fastapi提供)
![alt text](image-6.png)
## 声明元数据
>item_id: Annotated[int, Path(title="The ID of the item to get")],

Annotated(typing提供):一个打包工具,把参数的数据类型和校验规则绑在一起,结构如下

Annotated[基础类型, 附加信息1, 附加信息2, ...]

基础类型:数据类型

附加信息就是校验规则,注释等

## 按需对参数进行排序

如果将带有「默认值」的参数放在没有「默认值」的参数之前,Python 将会报错。

但是可以对其重新排序,并将不带默认值的值(查询参数 q)放到最前面。

对 FastAPI 来说这无关紧要。它将通过参数的名称、类型和默认值声明(Query、Path 等)来检测参数,而不在乎参数的顺序。
## 数值校验
ge:大于等于

gt:大于

lt:小于

le:小于等于

>async def read_items(
>
> item_id: Annotated[int, Path(title="The ID of the item to get", ge=1)], q: str
>
>):

加入附加条件ge=1,item_id必须为大于等于1的整数。(*ps:这里的路径参数本身就是必选参数,q因为没有默认值所以也为必选参数*)

Loading