Simulate logon using Python

Source: Internet
Author: User
Python login Simulation Implementation
This article mainly uses python to simulate website logon. Construct post data by yourself to implement the login process using python.
When you want to simulate a website login, you must first understand the details of the website login process (what kind of data is sent, who is sent, etc ...). I use httpfox to capture HTTP packets to analyze the login process of the website. At the same time, we also need to analyze the data structure and header of the captured post package, and construct our own post data and header Based on the submitted data structure and heander.
After the analysis, we need to construct our own HTTP packet and send it to the specified URL. We use APIs provided by several modules such as urllib2 to send and receive request requests. Most websites need to carry cookies when logging on, so we must also set cookie processors to ensure cookies.
The code and explanation are as follows:
#! /Usr/bin/pythonimport htmlparserimport urlparseimport urllibimport urllib2import cookielibimport stringimport re # log on to the home page with hosturl = ****** '// enter your own # post data receiving and processing page (we want to send the constructed post data to this page) posturl = '*******' // analyzes data packets and processes the POST request URL # sets a cookie processor, which is responsible for downloading cookies from the server to the local machine, the local cookiecj = cookielib is included in the request. lwpcookiejar () cookie_support = urllib2.httpcookieprocessor (CJ) opener = urllib2.build _ opener (cookie_support, u Rllib2.httphandler) urllib2.install _ opener (opener) # Open the logon homepage (the purpose is to download the cookie from the page, so that we will have a cookie when sending the post data, otherwise it will not be sent successfully) H = urllib2.urlopen (hosturl) # construct the header. Generally, the header must contain at least two items. These two items are analyzed from the captured bag. Headers = {'user-agent': 'mozilla/5.0 (Windows NT 6.1; wow64; RV: 14.0) Gecko/20100101 Firefox/14.0.1 ', 'Referer ': '*******'} # construct the post data, which is also obtained from the analysis in a large bag. Postdata = {'op': 'dmlogin', 'F': 'st', 'user ':'******', // your username 'pass': ******* ', // your password. The password may be transmitted in plaintext or ciphertext, for ciphertext data, you need to call the corresponding encryption algorithm to encrypt 'rmbr ': 'true', // special data. different websites may have different 'tmp': '0. 7306424454308195 '// special data, different websites may be different} # post data must be encoded with postdata = urllib. urlencode (postdata) # Send the constructed data to the specified URL through the request method provided by urllib2, and complete the login process request = urllib2.request (posturl, postdata, headers) print requestresponse = urllib2.urlopen (request) TEXT = response. read () print text

The above is my summary. If you have any questions, please correct them and hope to exchange and learn from each other!

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.