快速了解Python开发中的cookie及简单代码示例
作者:kesungang 时间:2023-05-29 11:04:05
cookie :是用户保存在用户浏览器端的一对键值对,是为了解决http的无状态连接。服务端是可以把 cookie写到用户浏览器上,用户每次发请求会携带cookie。
存放位置:
每次发请求cookie是放在请求头里面的。
应用场景:
·登陆用户和密码的记住密码
·显示每页显示的数据,以后都是按照设定的数目显示
·投票机制
案例用户登录
创建用户登录的url
url(r'^login/', views.login),
创建登录页面
代码为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆页面</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css" rel="external nofollow" >
<link rel="stylesheet" href="/static/jquery-3.2.1.min.js" rel="external nofollow" >
</head>
<body>
<form action="/login/" method="post">
<input type="text" name="username" placeholder="用户名">
<input type="text" name="password" placeholder="密码">
<input type="submit" value="提交">
</form>
</body>
</html>
创建后台函数方法
代码为:
def login(request):
if request.method == "GET":
return render(request,'houtaiguanli/login.html')
else:
user = request.POST.get('username')
pwd = request.POST.get('password')
if user == 'ke' and pwd == '123':
obj = redirect('/classes/')
obj.set_cookie('ticket','dafadfafaf')
return obj
else:
return render(request,'houtaiguanli/login.html')
这里设计登录成功后可以正常访问班级页面即“classes”,所以要在班级方法那里做个判断。如果判断为用户已经登录,则可以正常访问。判断用户没有正常登录,则让它跳转到登录页面继续登录
这里就是通过设置cookie,发送给用户的浏览器。用浏览器在进行新的请求时携带着cookie来的。
set_cookies的语法
Set-cookie:name=name;expires=date;path=path;domain=domain;secure name=name: 需要设置cookie的值(name不能使用“;”和","号),有多个name值时用";"分隔例如:name1=name1; name2=name2;name3=name3。
expires=date: cookie的有效期限,格式: expires="Wdy,DD-Mon-YYYY HH:MM:SS"
path=path: 设置cookie支持的路径,如果path是一个路径,则cookie对这个目录下的所有文件及子目录生效,例如:path="/cgi-bin/",如 果path是一个文件,则cookie指对这个文件生效,例如:path="/cgi-bin/cookie.cgi"。
domain=domain: 对cookie生效的域名,例如:domain="gzdzw.51.net"
secure: 如果给出此标志,表示cookie只能通过SSL协议的https服务器来传递。
cookie的接收是通过设置环境变量HTTP_COOKIE来实现的,CGI程序可以通过检索该变量获取cookie信息。
cookie 也可以做加密的
添加salt
后台拿去的时候解密
浏览器查看
来源:http://blog.51cto.com/sgk2011/2060238?utm_source=oschina-app
标签:python,cookie
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
跟老齐学Python之print详解
2021-02-06 03:32:56
php简单防盗链验证实现方法
2023-09-12 04:55:15
python计算最小优先级队列代码分享
2022-12-05 03:26:50
tp5递归 无限级分类详解
2024-05-11 09:53:48
9行Python3代码实现批量提取PDF文件的指定内容
2023-02-12 03:22:41
![](https://img.aspxhome.com/file/2023/8/88858_0s.png)
SQLServer无法打开用户默认数据库 登录失败错误4064的解决方法
2024-01-19 06:59:47
![](https://img.aspxhome.com/file/2023/2/113542_0s.jpg)
Javascript中的基本类型和引用类型概述说明
2024-04-18 09:37:04
![](https://img.aspxhome.com/file/2023/0/136370_0s.png)
Python使用django获取用户IP地址的方法
2021-07-22 04:01:05
SqlServer将数据库中的表复制到另一个数据库
2024-01-22 11:31:33
![](https://img.aspxhome.com/file/2023/4/89704_0s.png)
Python中tqdm的使用和例子
2022-05-31 23:59:32
![](https://img.aspxhome.com/file/2023/1/127821_0s.gif)
Python使用内置函数setattr设置对象的属性值
2021-10-09 20:32:44
python 监听salt job状态,并任务数据推送到redis中的方法
2022-09-14 05:19:47
使用Python的Twisted框架编写非阻塞程序的代码示例
2021-01-22 16:20:51
关于jupyter打开之后不能直接跳转到浏览器的解决方式
2023-07-17 01:39:55
![](https://img.aspxhome.com/file/2023/2/63532_0s.jpg)
CSS样式设计技巧十则
2008-06-04 12:18:00
Python实现数据集划分(训练集和测试集)
2022-11-29 23:54:11
pytorch使用 to 进行类型转换方式
2022-10-05 23:28:27
![](https://img.aspxhome.com/file/2023/6/101576_0s.jpg)
Php+SqlServer实现分页显示
2023-11-20 05:02:24
Oracle9i在Win2k环境下的完全卸载
2010-07-28 13:03:00
Golang sync.Pool的源码解析
2024-02-11 10:32:59