Python form login processing (1)

Python form login process and cookie (1)

1. Handling Login Form

Sign form a two-step process:

  1. Login Form research site, build parameters dictionary POST request
  2. Submits a POST request to sign

(1) Research website Login Form
Site http://www.santostang.com/wp-login.php for example (Account: test, Password: a12345)
Here Insert Picture Description
found by detecting the element
Here Insert Picture Description
name attribute user name input box of the value of log this will be a dictionary key, value for the user to enter
Here Insert Picture Description
a value of pwd password box name attribute
Here Insert Picture Description
key value is the value remember name attribute, value is inside forever
Here Insert Picture Description
some key value is set up hidden in a browser sign-in form value is not going to show up, we can find elements in the review
role:

A hidden field in the page is not visible to the user, is inserted into a hidden field in the form object to collect or transmit information, to facilitate the process the form is used by the program. When viewers click the Send button to send the form, the hidden field information is also sent to the server together.

2 Sometimes we give the user a message that he should submit up when the form is submitted in order to determine the identity of the user, such as sessionkey, and so on. Of course, these things can be achieved cookie, but the use of hidden fields on more simple. And will not have browser does not support, users disable cookie troubles.

3 Sometimes there are more than a form submit button, how to make the program user is able to distinguish in the end that a press submit button it up? We can write a hidden field, and then add a button at each οnclick = "document.form.command.value =" xx "after" Then we check the data received command value is based on the user will know that button to submit up.

4 Sometimes there is more than a web page form, we know more form is not submitted at the same time, but sometimes these interactions form indeed, we can add hidden fields in the form to make them linked.

5 JavaScript does not support global variables, but sometimes we have to use global variables, we can hide the value of pre-existing domain, its value will not be lost.

6 there is an example, such a press button to pop up four small window, wherein when the click of a small window three other self-closing. But IE does not support widgets call each other, so only write a hidden field in the parent window, when a small window to see the value of the hidden field is close to switch off their own.
(2) Construction of the POST request parameter dictionary
from the first step of the analysis parameters may be constructed POST request dictionary dict, as follows:

postdata = {
	'pwd' : 'a12345',
	'log' : 'test',
	'remember' : 'forever',
	'redirect_to' : 'http://www.santostang.com/wp-admin',
	'testcookie' : 1,
}

(3) submits a POST request
first need to import requests library, create a session object:

import requests
session = requests.session()

Web site development session is an important concept that is popular browsers to run this process closed, session objects store properties and configuration information for a particular user session needed.
code show as below:

import requests
session = requests.session()

post_url = 'http://www.santostang.com/wp-login.php' 
agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
headers = {
	'Host' : 'www.santostang.com',
	'Origin' : 'http://www.santostang.com',
	'Referer' : 'http://www.santostang.com/wp-login.php',
	'User-Agent' : agent
}
postdata = {
	'pwd' : 'a12345',
	'log' : 'test',
	'remember' : 'forever',
	'redirect_to' : 'http://www.santostang.com/wp-admin',
	'testcookie' : 1,
}
login_page = session.post(post_url,data=postdata,headers=headers)
print(login_page.status_code)
Published 33 original articles · won praise 1 · views 2297

Guess you like

Origin blog.csdn.net/qq_40805620/article/details/99199738