Requests request library exercise -- GitHub login

# coding = utf-8 
""" 
Combined with the package capture tool, two methods are used to simulate logging in to github 
directly using session login and using requests to log
in""" import requests import re #Set request headers, pretend to be browser headers = { ' Host ' : ' github.com ' , ' Connection ' : ' keep-alive ' , ' Cache-Control ' : ' max-age=0 ' , ' Origin ' : ' https://github.com ' , ' Upgrade-Insecure-Requests ' : ' 1 ' , ' Content-Type ': 'application/x-www-form-urlencoded', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', } def get_info(): """ Visit the github page to get the parameters required for post submission :return: """ url = ' https://github.com/login ' r = requests.get(url, verify= False) #Return the source code obtained after visiting github text = r.text #Through the regular, match the parameters that need to be submitted when the post is submitted #Analyze through the packet capture tool, the token parameter is required here token = re.findall(r ' <input type="hidden" name="authenticity_token" value="(.* ?)" /> ' , text, re.S) return r.cookies, token[0] if __name__ == ' __main__ ' : # """ #Use session and requests to simulate logging in to github, # """ # # Method 1: log in through session## Create a session, keep the session # session = requests. session() # # # Visit the login page and get cookies # rr = session.get('https://github.com/login', verify=False) # # # Change the parameter token value through regular matching # token = re. findall(r'<input type="hidden" name="authenticity_token" value="(.*?)" />', rr.text, re.S) # # # Through the packet capture tool, analyze the submitted parameters, and modify the changed parameters to those obtained by the code # # For example, token parameters # data = { # 'utf8': ' ✓', # 'password': 'zhao0.0002', # 'login': 'zInPython', # 'commit': 'Sign in', # 'authenticity_token': token[0 ], # } # # # Access the target address of the parameter submission, and pass the required parameters in # post_url = 'https://github.com/session' # r = session.post(post_url, data=data, headers=headers , verify=False) # # # Return the source code after successful login # print(r.text) #Method 2: Access github through requests #Get cookies and variable parameter cookies, token = get_info() #Construct the parameters required for post submission data = { ' utf8 ' : ' ' , ' password ' : ' zhao0.0002 ' , ' login ' : ' zInPython ' , ' commit ' : ' Sign in ' , ' authenticity_token ' :token, } #Access the login submission parameter URL post_url = ' https://github.com/session ' r = requests.post(post_url, data=data, headers=headers, cookies=cookies, verify= False) #Source code print after successful login (r.text)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325314677&siteId=291194637