1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
一、http请求周期
浏览器(socket客户端):
2.socket
.connect(ip,端口)
3.socket
.send("
规则: http协议
GET请求:
"GET /index.html?k1=1&k2=2 Http/1.1\r\nhost:www.xiaohuar.com\r\ncontent-type: application/json\r\n\r\n"
请求头和请求体使用\r\n\r\n分割, 前面是请求头,后面是请求提
POST请求:
"POST /index.html?k1=1&k2=2 Http/1.1\r\nhost:www.xiaohuar.com\r\ncontent-type: application/json\r\n\r\nusername=alex&pwd=123"
请求头和请求体使用\r\n\r\n分割,前面是请求头,后面是请求体
6.
获取响应
响应头,响应体
=
data.split(
'\r\n\r\n'
)
7.
断开连接
web端:
1.
服务端运行: ip,端口
4.
字符串
=
server.recv()
头,体
=
data.split(
"\r\n\r\n"
)
5.
服务端响应.
conn.send(
'......'
)
响应头:
响应体:
7.
断开连接
总结:
a.Http请求中本质都是字符串
b.Http请求短链接(请求,响应断开连接)
c.请求和响应都有: 头、体
请求:
请求头
\r\n\r\n
请求体
响应:
响应头
\r\n\r\n
响应体
<html>
....
<
/
html>
二、django框架
Django本质:
socket(wsgiref)
解析和封装http请求(
*
)
视图函数:
def
index(request):
request.GET
request.body
#获取原生的请求体
request.POST
#如果请求头中: content-type: urlencode-form...才将request.body转化成字典
可能有值,可能没值
request.method
request.Meta
request.GET.get()
request.GET.getlist()
request.POST.get()
request.POST.getlist()
return
HttpResponse(
'字符串/字节'
)
return
render(request,
"html路径"
, {})
return
redirect(
'URL'
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
三、cookie和session
cookie是客户端浏览器请求服务端登录时,服务端生成的一段键值对携带在响应头中,下
次客户端请求时,服务端会先验证cookie是否存在,存在则不需要重新登录,否则要从新登录
session是客户端登录服务端时,验证通过后会随机生成一段字符串作为key存放在django.session表中,
值是也是一个字典,是程序设定的一些用户信息,同时响应头会携带这个key返回给客户端,这样会更加
安全。
session认证登录退出流程:
def
auth(func):
def
inner(request,
*
args,
*
*
kwargs):
ck
=
request.session.get(
'uuuuuuuu'
)
if
not
ck:
return
redirect(
'/login.html'
)
return
func(request,
*
args,
*
*
kwargs)
return
inner
@auth
def
index(request):
user
=
request.session.get(
'uuuuuuuu'
)
return
render(request,
'index.html'
,{
'user'
: user})
def
login(request):
if
request.method
=
=
"GET"
:
return
render(request,
'login.html'
)
else
:
user
=
request.POST.get(
'user'
)
pwd
=
request.POST.get(
'pwd'
)
pwd
=
encrypt(pwd)
obj
=
models.userInfo.objects.
filter
(username
=
user,password
=
pwd).first()
if
obj:
request.session[
'uuuuuuuu'
]
=
user
#存放在django.session中这个用户key对应的字典值中
request.session.set_expiry(
5
)
return
redirect(
'/index.html'
)
else
:
return
render(request,
'login.html'
,{
'msg'
:
'用户名或密码错误'
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
def
logout(request):
request.session.clear()
return
redirect(
'/login.html'
)
session有关配置(settings.py配置文件)
# SESSION_ENGINE = 'django.contrib.sessions.backends.db' # 引擎(默认)
#
# SESSION_COOKIE_NAME = "sessionid" # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串(默认)
# SESSION_COOKIE_PATH = "/" # Session的cookie保存的路径(默认)
# SESSION_COOKIE_DOMAIN = None # Session的cookie保存的域名(默认)
# SESSION_COOKIE_SECURE = False # 是否Https传输cookie(默认)
# SESSION_COOKIE_HTTPONLY = True # 是否Session的cookie只支持http传输(默认)
# SESSION_COOKIE_AGE = 1209600 # Session的cookie失效日期(2周)(默认)
# SESSION_EXPIRE_AT_BROWSER_CLOSE = False # 是否关闭浏览器使得Session过期(默认)
# SESSION_SAVE_EVERY_REQUEST = True # 是否每次请求都保存Session,默认修改之后才保存(默认)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
四、csrf_token
跨站请求伪造,post提交数据为避免其他网站通过用户session信息获取信息,
需要使用{
%
csrf_token
%
},
在ajax提交时,可以使用以下方式:
$.ajax({
url:
"/icbc.html"
,
type
:
"POST"
,
data: {
'k1'
:
'v1'
,
'k2'
:
'v2'
,
'csrfmiddlewaretoken'
:
"{{ csrf_token }}"
},
success:function(arg){
console.log(arg)
}
})
五、分页
urls.py
from
app01
import
views
urlpatterns
=
[
url(r
'^admin/'
, admin.site.urls),
url(r
'hosts.html$'
, views.hosts),
]
views.py
from
django.shortcuts
import
render,redirect,HttpResponse
from
app01
import
models
# Create your views here.
from
utils.pagination
import
Page
def
hosts(request):
current_page
=
int
(request.GET.get(
'page'
))
all_count
=
models.Host.objects.
all
().count()
page_obj
=
Page(current_page,all_count,request.path_info)
host_list
=
models.Host.objects.
all
()[page_obj.start:page_obj.end]
page_str
=
page_obj.page_html()
return
render(request,
'hosts.html'
,{
'host_list'
:host_list,
'page_str'
:page_str})
models.py
#数据库自己填充足够数据测试
from
django.db
import
models
# Create your models here.
class
Host(models.Model):
hostname
=
models.CharField(max_length
=
32
)
port
=
models.CharField(max_length
=
10
)
分页模块
pagination.py
class
Page:
def
__init__(
self
,current_page, all_count, base_url,per_page
=
10
, pager_page_count
=
11
):
"""
:param current_page: 当前页码
:param all_count: 总数据条数
:param base_url:
:param per_page: 每页显示数据条数
:param pager_page_count: 页面显示页码数
"""
self
.base_url
=
base_url
self
.current_page
=
current_page
self
.per_page
=
per_page
self
.pager_page_count
=
pager_page_count
pager_count, b
=
divmod
(all_count,per_page)
#pager_count总页码数
if
b !
=
0
:
pager_count
+
=
1
self
.pager_count
=
pager_count
half_pager_page_count
=
int
(pager_page_count
/
2
)
self
.half_pager_page_count
=
half_pager_page_count
@
property
def
start(
self
):
"""
数据获取值起始索引
:return:
"""
return
(
self
.current_page
-
1
)
*
self
.per_page
@
property
def
end(
self
):
"""
数据获取值结束索引
:return:
"""
return
self
.current_page
*
self
.per_page
def
page_html(
self
):
"""生成html页面"""
#如果数据总页码pager_count<11 pager_page_count
if
self
.pager_count <
self
.pager_page_count:
pager_start
=
1
pager_end
=
self
.pager_count
else
:
# 数据页码已经超过11
# 判断: 如果当前页 <=5 half_pager_page_count
if
self
.current_page <
=
self
.half_pager_page_count:
pager_start
=
1
pager_end
=
self
.pager_page_count
else
:
# 如果: 当前页码+5 > 总页码
if
(
self
.current_page
+
self
.half_pager_page_count) >
self
.pager_count:
pager_end
=
self
.pager_count
pager_start
=
self
.pager_count
-
self
.pager_page_count
+
1
else
:
pager_start
=
self
.current_page
-
self
.half_pager_page_count
pager_end
=
self
.current_page
+
self
.half_pager_page_count
page_list
=
[]
if
self
.current_page <
=
1
:
prev
=
'<a href="#">上一页</a>'
else
:
prev
=
'<a href="%s?page=%s">上一页</a>'
%
(
self
.base_url,
self
.current_page
-
1
,)
page_list.append(prev)
for
i
in
range
(pager_start, pager_end
+
1
):
if
self
.current_page
=
=
i:
tpl
=
'<a class="active" href="%s?page=%s">%s</a>'
%
(
self
.base_url, i, i,)
else
:
tpl
=
'<a href="%s?page=%s">%s</a>'
%
(
self
.base_url,i,i,)
page_list.append(tpl)
if
self
.current_page >
=
self
.pager_count:
nex
=
'<a href="#">下一页</a>'
else
:
nex
=
'<a href="%s?page=%s">下一页</a>'
%
(
self
.base_url,
self
.current_page
+
1
,)
page_list.append(nex)
page_str
=
"".join(page_list)
return
page_str
测试页面hosts.html
<!DOCTYPE html>
<html lang
=
"en"
>
<head>
<meta charset
=
"UTF-8"
>
<title>Title<
/
title>
<style>
.pagination1 a{
display: inline
-
block;
padding:
5px
8px
;
background
-
color: darkslateblue;
margin:
5px
;
color: white;
}
.pagination1 a.active{
background
-
color: green;
}
<
/
style>
<
/
head>
<body>
<h1>主机列表<
/
h1>
<table border
=
"1"
>
<thread>
<tr>
<th>
ID
<
/
th>
<th>主机名<
/
th>
<th>端口<
/
th>
<
/
tr>
<
/
thread>
<tbody>
{
%
for
row
in
host_list
%
}
<tr>
<td>{{ row.
id
}}<
/
td>
<td>{{ row.hostname }}<
/
td>
<td>{{ row.port }}<
/
td>
<
/
tr>
{
%
endfor
%
}
<
/
tbody>
<
/
table>
<div
class
=
"pagination1"
>
{{ page_str|safe }}
<
/
div>
<
/
body>
<
/
html>
|
本文转自小白的希望 51CTO博客,原文链接:http://blog.51cto.com/haoyonghui/1962423
,如需转载请自行联系原作者