Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle analysis of Python simulating landing on each website by grabbing the package?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to realize the principle analysis of Python simulation landing on each website by grasping the package. The content of the article is of high quality, so the editor will share it for you to do a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

A brief introduction to the tutorial

1.1 basic introduction (Python learning materials can be obtained by any one of 001,002,003,007)

By analyzing the login process and using Python to simulate login to a website provided by the experiment, we will learn and practice the network programming of Python, the method of simulated login by Python, and use Firefox packet analysis plug-in to analyze network data packets.

Simulated login can help users automate many operations, and it has different uses in different situations, whether it is automating some daily tedious operations or for crawlers is a very practical skill. This course is implemented through Firefox and Python, and the environmental requirements are as follows:

Python library: urllib, http.cookiejar, Django

Firefox requirement: live http header plug-in installed (provided)

1.3 Materials

1.4 preparation

1) install the package capture plug-in Live Http Headers

2) start the web application

Use the ps-aux | grep codebox query to get the process number of codebox, and then use the kill-9 process number to stop the codebox process. The execution process is shown in the following figure:

First install the web framework django that demo depends on, and test whether the installation is successful:

After starting successfully, enter http://localhost:8000/polls in the browser and see the login page to indicate that the startup is successful.

Second, analyze the login process

To log in by programming, we first need to understand the login process of general Web applications.

2.1 grab request

2.2 principle analysis

Why two requests instead of one? Through the analysis of the login request, it is found that after the successful login, the server sent a 302 redirect response, and the server asked the browser to rerequest the home page, which resulted in a second request. If you analyze the second request, you can see that it has one more request header than the login request:

The entire login process is shown in the following figure:

How exactly does the server distinguish the session of different users? Why write back cookie after a successful login meeting?

2.3 Summary

For the server, login = verify + write session. For browsers, login = send login information + get cookie with sessionid. It can be said that as long as you get the sessionid, you can achieve a simulated login. With it, we can dissociate from the system.

Use Python to log in (simple example)

After understanding the principles and details of the login process, start using Python to write a simulated login program.

3.1 Import module

Don't forget to write the file header and import the necessary dependent modules

3.2 construct a login request

The entity portion of the login request is as follows:

All Python codes:

Additional parameters

Hotlink protection

The resources of Web applications all have url, and as long as you get the url, you can reference it anywhere. It sounds convenient, but it may cause your resources to be embezzled by others.

3.3 send the request and save the cookie

The Python code is as follows:

If the login is successful

You can see the sessionid in the specified file my_cookies.txt.

3.4.Using cookie to access system services

After our simulated login is successful, we can open the url directly through opener to use the system service. The code is implemented as follows:

If you are using this cookie in another python file, you need to load cookie before opening url:

On the realization of Python simulation landing through the package of the principle analysis is shared here, I hope that the above content can be of some help to everyone, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report