Obtain the verification code address during browsing and capture the interface before redirection

1. Right-click on the verification code picture to copy the picture address http://xxx.yanzhengma.com
2. 1) Grab the protected data during browsing-the url before the 302 redirection, you need to check the preserver log option 2 ) Form data: written in the form of a dictionary
3. Login with verification code: 1) Because a request to generate a verification code is sent to the server before logging in, the server generates a cookie for the verification code to distinguish the unique identifier of the verification code, because The verification code can be repeated. Therefore, the server will return a verification code cookie,
login request + verification code cookie, to log in successfully
2) Verification code request
: http://xxx.yanzhengma.com = "http://xxx. yanzhengma.com"
rep = r.get(http://xxx.yanzhengma.com)
r_cookie = rep.cookies # is a dictionary format, directly take a key value
Note: set the code_cookies of the incoming parameter = {"laravel_session": r_cookie ['laravel_session']}
Put cookies in the login request, as follows:
res = request.post(url = url ,data =data, cookies = code_cookies) 4.
1) Benefits of using the session object: session can automatically save the server generated Cookies information, and automatically appended to the next request
2) What is a session: a session (starting from the creation of a request connection between the client and the server, and the end of the disconnection between the client and the server, that is, closing the browser)
3) How to use:
session = requests.session() # 创建session对象
res = session.get(url)…session.post(url,data)等put、delete操作
4)在unitest登录的实践
import unittest
class Test_unittest(unittest.TestCase):
def setUp(self):
self.session = requests.session()
self.url_login = “http://xxx.login.com”
self.url_code = “http://xxx.url_code.com”

def tearDown(self):
    self.session.close()
def test_login_success(self):
    self.session.get(self.url_code)
    data = {"name":"admin",
            "password":"admin"}
    res = self.session.post(self.url_login,data = data)
    try:
        self.assertEqual("o",res.json()['code'])
    except AssertionError as e:
        print(e)

def test_login_password_error(self):
    self.session.get(self.url_code)
    data = {"name":"admin",
            "password":"000"}
    res = self.session.post(self.url_login,data = data)
    try:
        self.assertEqual("o",res.json()['code'])
    except AssertionError as e:
        print(e)
if __name__ == '__main__':
    unittest.main()    

Guess you like

Origin blog.csdn.net/qq_37405087/article/details/110086476