


How to use Python's Requests package to implement simulated login
This article mainly introduces in detail the use of Python's Requests package to simulate login. It has a certain reference value. Interested friends can refer to it
I liked to use python to capture some pages some time ago. Play, but they basically use get to request some pages, and then filter them through regular rules.
I tried it today and simulated logging into my personal website. The discovery is also relatively simple. Reading this article requires a certain understanding of the http protocol and http sessions.
Note: Because the simulated login is my personal website, the following code handles the personal website and account password.
Website Analysis
The essential first step for crawlers is to analyze the target website. Here we use Google Chrome’s developer tools for analysis.
Fetch through login and see such a request.
The upper part is the request header, and the lower part is the parameters passed by the request. As can be seen from the picture, the page submits three parameters through the form. They are _csrf, usermane, password respectively.
The csrf is to prevent cross-domain script forgery. The principle is very simple, that is, for every request, the server generates an encrypted string. Place it in a hidden input form. When making another request, pass this string together to verify whether it is a request from the same user.
So, our code logic is there. Start by requesting a login page. Then analyze the page and get the csrf string. Finally, this string and the account password are passed to the server for login.
The first code
#!/usr/bin/env python2.7 # -*- coding: utf-8 -*- import requests import re # 头部信息 headers = { 'Host':"localhost", 'Accept-Language':"zh-CN,zh;q=0.8", 'Accept-Encoding':"gzip, deflate", 'Content-Type':"application/x-www-form-urlencoded", 'Connection':"keep-alive", 'Referer':"http://localhost/login", 'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36" } # 登陆方法 def login(url,csrf): data = { "_csrf" : csrf, "username": "xiedj", "password": "***" } response = requests.post(url, data=data, headers=headers) return response.content # 第一次访问获取csrf值 def get_login_web(url): page = requests.get('http://localhost/login') reg = r'<meta name="csrf-token" content="(.+)">' csrf = re.findall(reg,page.content)[0] login_page = login(url,csrf) print login_page if __name__ == "__main__": url = "http://localhost/login/checklogin" get_login_web(url)
The code seems to have no problem. However, an error occurred during execution. After checking, the reason for the error is that the csrf verification failed!
After repeatedly confirming that the csrf obtained and the csrf string requested to log in were OK, I thought of a problem.
If you still don’t know the cause of the error, you can pause and think about a problem here. "How does the server know that the first request to obtain csrf and the second post login request are from the same user?"
At this point, it should be clear. If you want to log in successfully, you need to solve how to make the service believe that both The requests are from the same user. You need to use http session here (if you are not sure, you can Baidu yourself, here is a brief introduction).
The http protocol is a stateless protocol. To make this stateless become stateful, sessions were introduced. To put it simply, record this status through the session. When a user requests a web service for the first time, the server will generate a session to save the user's information. At the same time, when returning to the user, the session ID is saved in cookies. When the user requests again, the browser will bring this cookie with it. Therefore, the server can know whether multiple requests are for the same user.
So our code needs to get this sessionID when making the first request. Pass this sessionID together with the second request. The great thing about requests is that you can use this session object with a simple request.Session().
The second code
#!/usr/bin/env python2.7 # -*- coding: utf-8 -*- import requests import re # 头部信息 headers = { 'Host':"localhost", 'Accept-Language':"zh-CN,zh;q=0.8", 'Accept-Encoding':"gzip, deflate", 'Content-Type':"application/x-www-form-urlencoded", 'Connection':"keep-alive", 'Referer':"http://localhost/login", 'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36" } # 登陆方法 def login(url,csrf,r_session): data = { "_csrf" : csrf, "username": "xiedj", "password": "***" } response = r_session.post(url, data=data, headers=headers) return response.content # 第一次访问获取csrf值 def get_login_web(url): r_session = requests.Session() page = r_session.get('http://localhost/login') reg = r'<meta name="csrf-token" content="(.+)">' csrf = re.findall(reg,page.content)[0] login_page = login(url,csrf,r_session) print login_page if __name__ == "__main__": url = "http://localhost/login/checklogin" get_login_web(url)
The page after successful login
You can know from the code that after requests.Session() starts the session object, the second request will automatically pass the last session ID together.
Related recommendations:
How to use Python to export Excel charts and export them as pictures
Analyze the open function using python Reasons for the No Such File or DIr error
The above is the detailed content of How to use Python's Requests package to implement simulated login. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
