| 8 | |
| 9 | |
| 10 | class GithubLogin(object): |
| 11 | |
| 12 | def __init__(self, email, password): |
| 13 | # 初始化信息 |
| 14 | self.headers = { |
| 15 | 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36', |
| 16 | 'Referer': 'https://github.com/', |
| 17 | 'Host': 'github.com' |
| 18 | } |
| 19 | |
| 20 | self.session = requests.Session() |
| 21 | self.login_url = 'https://github.com/login' |
| 22 | self.post_url = 'https://github.com/session' |
| 23 | self.email = email |
| 24 | self.password = password |
| 25 | |
| 26 | def login_GitHub(self): |
| 27 | # 登录入口 |
| 28 | post_data = { |
| 29 | 'commit': 'Sign in', |
| 30 | 'utf8': '✓', |
| 31 | 'authenticity_token': self.get_token(), |
| 32 | 'login': self.email, |
| 33 | 'password': self.password |
| 34 | } |
| 35 | resp = self.session.post( |
| 36 | self.post_url, data=post_data, headers=self.headers) |
| 37 | |
| 38 | print('StatusCode:', resp.status_code) |
| 39 | if resp.status_code != 200: |
| 40 | print('Login Fail') |
| 41 | match = re.search(r'"user-login" content="(.*?)"', resp.text) |
| 42 | user_name = match.group(1) |
| 43 | print('UserName:', user_name) |
| 44 | |
| 45 | |
| 46 | |
| 47 | # Get login token |
| 48 | def get_token(self): |
| 49 | |
| 50 | response = self.session.get(self.login_url, headers=self.headers) |
| 51 | |
| 52 | if response.status_code != 200: |
| 53 | print('Get token fail') |
| 54 | return None |
| 55 | match = re.search( |
| 56 | r'name="authenticity_token" value="(.*?)"', response.text) |
| 57 | if not match: |
| 58 | print('Get Token Fail') |
| 59 | return None |
| 60 | return match.group(1) |
| 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |