Python implements simulated website login, and python simulates
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/python import HTMLParser import urlparse import urllib import urllib2 import cookielib import string import re # log on to the home page hosturl = ******* '// enter it by yourself # post page for receiving and processing data (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 cookie cj = cookielib is included in the request. LWPCookieJar () cookie_support = urllib2.HTTPCookieProcessor (cj) opener = urllib2.build _ opener (co Okie_support, urllib2.HTTPHandler) urllib2.install _ opener (opener) # Open the logon homepage (which aims to download cookies from the page so that we will have cookies when sending post data again, otherwise, the message cannot be sent.) 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 request response = urllib2.urlopen (request) text = response. read () print text
The above is all about this article. I hope you can exchange and learn from each other and make progress together!
Articles you may be interested in:
- Python remote logon code
- Python uses Socket (Https) Post to log on to Baidu.
- Python implementation code for batch logon and command execution via ssh
- A small example of Python User Logon Verification
- Use webqq protocol to use python to log on to qq and send the source code reference
- Python uses the rsa Encryption Algorithm Module to simulate Sina Weibo Login
- A simple example of using the requests library to simulate login and capture data in python
- Python login 12306 Simulation Method
- Use cookielib in Python to simulate website login