Quick introduction to cookies and simple code examples in Python development, pythoncookie
Cookie: a key-Value Pair stored in the user's browser to solve the stateless connection of http. The server can write the cookie to the user's browser. Each request is sent with a cookie.
Storage location:
Each request cookie is placed in the request header.
Application scenarios:
· Remember the password for login users and passwords
· The data displayed on each page will be displayed in the future according to the set quantity
· Voting mechanism
Case User Login create user login url
url(r'^login/', views.login),
Create logon page
Code:
<! DOCTYPE html> Background function Creation Method
Code:
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')
After successful login, you can access the class page normally, that is, "classes". Therefore, make a judgment on the class method. If it is determined that the user has logged on, it can be accessed normally. Determine that the user is not logged on normally, so that it will jump to the logon page to continue logging on
The cookie is set and sent to the user's browser. The browser carries cookies when making new requests.
Set_cookies syntax Set-cookie: name = name; expires = date; path = path; domain = domain; secure name = name: the cookie value needs to be Set (name cannot use "; "and", "), separated by"; ", for example, name1 = name1; name2 = name2; name3 = name3. Expires = date: cookie validity period. Format: expires = "Wdy, DD-Mon-yyyy hh: MM: SS" path = path: set the path supported by cookies, if path is a path, the cookie takes effect for all files and subdirectories in this directory, for example, path = "/cgi-bin/". If path is a file, cookie is effective for this file, for example, path = "/cgi-bin/cookie. cgi ". Domain = domain: the domain name that takes effect for the cookie. For example, domain = "gzdz42451.net" secure: If this flag is provided, the cookie can only be transmitted through the https server of the SSL protocol. Cookie receiving is implemented by setting the environment variable HTTP_COOKIE. The CGI program can retrieve the cookie information by searching the variable.
Cookies can also be encrypted.
Add salt
Decryption when the backend is taken
Browser View
Summary
The above is all about the quick introduction to cookies and simple code examples in Python development. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!