By grasping the package, to achieve the Python simulation landing site, the principle of analysis!

Source: Internet
Author: User

First, Introduction to the tutorial

1.1 Basic Introduction

Through the analysis of the landing process and the use of Python to achieve the simulation landing to an experiment to provide Web site, in the experimental process will learn and practice Python network programming, Python implementation of the simulation method of landing, using the FIREFOX packet Analysis plug-in analysis of network data packets and other knowledge.

Analog login can help users to automate a lot of operations, in different situations have different uses, whether it is to automate some of the daily tedious operation or crawler is a very useful skill. This course is implemented via Firefox and Python, with the following environmental requirements:

    • Python Library: Urllib, Http.cookiejar, Django
    • Firefox requirements: Equipped with live HTTP header plugin (provided)

1.3 Materials

1.4 Preparation

1) Install the capture plugin live Http Headers

2) Start the web App

Using Ps-aux | The grep codebox query obtains the Codebox process number and then uses the kill-9 process number to stop the codebox process. The implementation process is shown in:

First install the demo-dependent web framework Django and test for the success of the installation:

After successful startup, enter Http://localhost:8000/polls in the browser to see the login page to indicate the success of the launch

Second, the analysis of the login process

To programmatically implement a login, you first need to understand the general Web application landing process.

2.1 Fetch Request

2.2 Principle Analysis

Why 2 requests instead of 1? By analyzing the login request, the server sends a 302 REDIRECT response after the login succeeds, and the server asks the browser to re-request the home page, which results in a second request. To parse the second request, you can see that it has one more request header than the login request:

The entire login process is as follows:

How exactly does the server differentiate between different users ' sessions? Why do I have to write a cookie back after the successful landing?

2.3 Summary

For the server, login = Verify + Write session. For browsers, login = Send login information + get cookies with SessionID. It can be said that, as long as the SessionID, even if the implementation of the analog login. With it we can drift away from the system.

Third, using Python to implement login (simple instance)

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

3.1 Importing Modules

Do not forget to write the file header, import the necessary dependent modules

3.2 Constructing a login request

The entity portion of the logon request is as follows:

All Python code:

Additional parameters

Anti-theft chain

Web app resources are URLs and can be referenced anywhere if they get a URL. It sounds convenient, but it can cause your resources to be stolen by others.

3.3 Sending requests and saving cookies

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 cookies to access system services

After we have successfully simulated login, we can open this URL directly through opener to use this system service. The code is implemented as follows:

If you are using this cookie in another Python file, you will need to load the cookie before opening the URL:

The tutorial was taken from the lab building.

By grasping the package, to achieve the Python simulation landing site, the principle of analysis!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.