Skip to content

Commit 3fd8fce

Browse files
committed
yes
1 parent 216da45 commit 3fd8fce

File tree

201 files changed

+54446
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

201 files changed

+54446
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

md/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
---
2+
show: step
3+
version: 1.0
4+
enable_checker: true
5+
---
6+
7+
# 前端和后端
8+
## 我们来回顾一下 😌
9+
10+
- 上次了解了 请求和响应
11+
- 请求就是客户发出的订单
12+
- 响应就是堂倌做出的反应
13+
- 堂倌会看人下菜碟
14+
- 来了请求
15+
- 能把浏览器基本信息都得到
16+
17+
- 请求和响应这套东西
18+
- 能做点什么呢?
19+
20+
### 翻译
21+
22+
- 比如说翻译
23+
24+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719050273346)
25+
26+
- 我发出中文请求
27+
- 要求得到英文响应
28+
29+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719050430756)
30+
31+
- 来试试
32+
33+
### 尝试翻译
34+
35+
- 安装包
36+
37+
```
38+
pip install translate
39+
```
40+
41+
- 传统本地翻译
42+
43+
```
44+
from translate import Translator
45+
46+
# 创建Translator对象
47+
translator = Translator(to_lang = 'zh')
48+
49+
# 进行翻译
50+
translation = translator.translate('Hello, world!')
51+
52+
# 获取翻译结果
53+
print(translation)
54+
```
55+
56+
- 运行结果
57+
58+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240426-1714108876045)
59+
60+
- 怎么把这套放到网上呢?
61+
62+
63+
### 构建flask应用服务器
64+
65+
```
66+
from flask import Flask
67+
68+
app = Flask(__name__)
69+
70+
@app.route('/')
71+
def index():
72+
return "hello"
73+
74+
if __name__ == "__main__":
75+
app.run(debug=True)
76+
```
77+
78+
- 访问首页成功
79+
80+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719055517723)
81+
82+
### 打造静态页面
83+
84+
```
85+
mkdir my_app
86+
cd my_app
87+
mkdir static
88+
vi static/pre.html
89+
tree
90+
```
91+
92+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719053831546)
93+
94+
- 构造网页
95+
- 由于本页面在static文件夹下
96+
- 这个url就是提交到向上一层
97+
- 即根下的trans
98+
- /trans
99+
100+
```
101+
<html lang="zh">
102+
<head>
103+
<title>preCalculate</title>
104+
</head>
105+
<body>
106+
<form method="GET" id="preCal" action="../trans">
107+
<input type="text" id="sen" name="sentence">
108+
<input type="submit" value="go"/>
109+
</form>
110+
</body>
111+
</html>
112+
```
113+
114+
- 注意此时 form 提交的方法为
115+
- GET
116+
117+
### 准备浏览网页
118+
119+
- 后台运行应用服务器
120+
121+
```
122+
nohup python3 app.py >> f.log 2>&1 &
123+
firefox http://localhost:5000/static/pre.html &
124+
```
125+
126+
- 查看结果
127+
128+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240426-1714121391803)
129+
130+
- 静态页
131+
- 访问成功
132+
133+
### 发送请求
134+
135+
- F12调出工具
136+
- 选择 网络 页面
137+
138+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719056077762)
139+
140+
- 点击 按钮
141+
- 发送请求
142+
143+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719056241461)
144+
145+
- 发送GET请求
146+
- 找不到该URI
147+
- 得到404状态码
148+
- 为什么会这样?
149+
150+
### 设置路由
151+
152+
- /trans没有对应的路由
153+
154+
```
155+
from flask import Flask
156+
from flask import request
157+
from translate import Translator
158+
159+
160+
app = Flask(__name__)
161+
162+
@app.route('/')
163+
def index():
164+
return "hello"
165+
166+
@app.route('/trans',methods=['POST',"GET"])
167+
def trans():
168+
s = request.args.get("sentence")
169+
translator = Translator(to_lang = 'zh')
170+
translation = translator.translate(s)
171+
return translation
172+
173+
if __name__ == "__main__":
174+
app.run(debug=True)
175+
176+
```
177+
178+
- 处理之后
179+
180+
### 准备再次提交
181+
182+
```
183+
firefox http://localhost:5000/static/pre.html &
184+
```
185+
186+
- 准备再次提交
187+
188+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719056783585)
189+
190+
### 提交结果
191+
192+
- 确实可以翻译
193+
194+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719056798728)
195+
196+
- 提交的参数都在uri里面
197+
- 写的清清楚楚
198+
- 这就是GET的提交方式
199+
- 还可以在uri中隐藏
200+
- 请求的数据吗?
201+
202+
### 修改页面
203+
204+
```
205+
vi static/pre.html
206+
```
207+
208+
- 把提交方式method
209+
- 从GET
210+
- 修改为POST
211+
212+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719057037006)
213+
214+
- 这样也能提交吗?
215+
216+
### 再次准备提交
217+
218+
219+
```
220+
firefox http://localhost:5000/static/pre.html &
221+
```
222+
223+
- 再次准备提交
224+
225+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719056783585)
226+
227+
### 提交结果
228+
229+
- 状态码500
230+
- 服务器内部错误
231+
232+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719062113177)
233+
234+
- 该如何修改?
235+
236+
### 修改app.py
237+
238+
```
239+
from flask import Flask
240+
from flask import request
241+
from translate import Translator
242+
243+
244+
app = Flask(__name__)
245+
246+
@app.route('/')
247+
def index():
248+
return "hello!"
249+
250+
@app.route('/trans',methods=['POST',"GET"])
251+
def trans():
252+
s = request.form["sentence"]
253+
print(s)
254+
translator = Translator(to_lang = 'zh')
255+
translation = translator.translate(s)
256+
return translation
257+
258+
if __name__ == "__main__":
259+
app.run(debug=True)
260+
```
261+
262+
- 可以看到url中的参数消失了
263+
- 方法确实是POST
264+
- 参数被放到了
265+
- 请求表单中
266+
267+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719062307468)
268+
269+
- 为什么要放请求表单呢?
270+
271+
### 安全性
272+
273+
![图片描述](https://doc.shiyanlou.com/courses/uid1190679-20240622-1719062571189)
274+
275+
### 总结 🤨
276+
- 这次应用了 请求和响应
277+
- 请求 就是 客户发出的 文字
278+
- 响应 就是 翻译官 翻译好的 文字
279+
- 后台就是翻译官
280+
- 可以用表单
281+
- 来做个webshell吗?
282+
- 下次再说!👋

0 commit comments

Comments
 (0)