Objective:
Log in before the blog is a JSON parameter, some logins are not JSON, such as Jenkins's login, this article with Jenkins Login as a case, the data parameters.
First, login Jenkins grab Bag
1. Login to Jenkins and enter your account number and password
2.fiddler Grab Bag
3. This body parameter is not in JSON format, it is the key=value format, which is the second of the four data types mentioned in the previous post request
Second, request the head
1. The above grab bag already knows the body data type, then the head inside the Content-type type also needs to fill in the corresponding parameter type
III. Realization of Login
1. The login code is as follows:
1 #Coding:utf-82 ImportRequests3 4 #Open the Login first page to get some cookies5URL ="Http://localhost:8080/jenkins/j_acegi_security_check"6headers = {7 "user-agent":"mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0"8}#Get method Add a ser-agent to it.9D = {" from":"",Ten "J_password":"f7bcd85ebab14e2fbb6d76cc99bc5c6a", One "J_username":"Admin", A "Jenkins-crumb":"e677c237181756818cbbccd4296d44f1", - "JSON": {"J_username":"Admin", - "J_password":"f7bcd85ebab14e2fbb6d76cc99bc5c6a", the "Remember_me": True, - " from":"", - "Jenkins-crumb":"e677c237181756818cbbccd4296d44f1"}, - "Remember_me":" on", + "Submit": U"Login" - } +s =requests.session () Ar = S.post (URL, headers=headers, data=d) at PrintR.content
2. Printing results
Iv. determine if the login was successful
1. First this login interface has redirect, see left Session box 302, the result of login success see the last 200 on the line
2. The returned result is not the same as the blog Garden JSON format, returned is an HTML page
V. To determine the success of login
1. To determine the success of the login, you can crawl the key elements on the page, such as: Account name admin, back logout button
2. Present these 2 keywords through regular expressions
Vi. Reference Code
1 #Coding:utf-82 ImportRequests3 4 #Open the Login first page to get some cookies5URL ="Http://localhost:8080/jenkins/j_acegi_security_check"6headers = {7 "user-agent":"mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0"8}#Get method Add a ser-agent to it.9D = {" from":"",Ten "J_password":"f7bcd85ebab14e2fbb6d76cc99bc5c6a", One "J_username":"Admin", A "Jenkins-crumb":"e677c237181756818cbbccd4296d44f1", - "JSON": {"J_username":"Admin", - "J_password":"f7bcd85ebab14e2fbb6d76cc99bc5c6a", the "Remember_me": True, - " from":"", - "Jenkins-crumb":"e677c237181756818cbbccd4296d44f1"}, - "Remember_me":" on", + "Submit": U"Login" - } +s =requests.session () Ar = S.post (URL, headers=headers, data=d) at - #Regular Expression Extract account and login button - ImportRe -t = Re.findall (r'<b> (. +?) </b>', R.content) - PrintT[0] - PrintT[1]
Python interface automation 11-post data parameter case