利用Python模拟GitHub登录
最近学习了Fiddler抓包工具的简单使用,通过抓包,我们可以抓取到HTTP请求,并对其进行分析。现在我准备尝试着结合Python来模拟GitHub登录。
Fiddler抓包分析
首先,我们想要模拟一个网站的登录,我们必须要简单了解其大致过程。
在这里,我通过Fiddler来抓取GitHub登录的请求,从网页上登录的URL为:https://github.com/login ,抓包结果如下:

左边的是会话列表,右边的是请求和响应的数据。一般情况下,登录都是用POST请求,因为我在左边的会话列表中设置了显示RequestMethod一列,因此能够很方便的找到POST请求。当然,Fiddler默认不显示RequestMethod,如果没有设置,还可以通过命令“=post”来快速过滤POST请求。

在GitHub登录时,我们通过抓包发现,GitHub登录的URL虽然时https://github.com/login,但发生了302重定向,其真正提交POST表单数据的URL是 https://github.com/session ,当登录成功时,则会跳转到 https://github.com/ 首页。
打开WebForm,我们可以看到POST表单数据提交的值,可以发现,只有authenticity_token、login、password三个字段是会变化的,其余的每次登录都是固定的值。而login、password分别是我们登录的用户和密码,因此我们只需要分析出 authenticity_token 从何而来,便可以实现模拟登录了。
至于如何确定 authenticity_token 从哪个页面返回的,我们直接在响应数据中搜索就行了,或者把数据复制出来再进行搜索。最后我们会发现,authenticity_token 是在 https://github.com/login 这个请求中返回的,只不过用 hidden 隐藏起来了。

好了,到目前大致流程我们已经梳理清楚了,接下来我们便通过Python来实现模拟GitHub登录。
代码实现
本人环境:PyCharm 2018.2.4、Python3.7.0
1. 设置请求头和Session
# 设置Session
self.s = requests.session()
# 设置请求头
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:44.0) Gecko/20100101 Firefox/44.0"
}
# 在使用Fiddler时进行请求,通过该代码忽略SSLError错误
self.s.verify = False
在这里,我们设置了Session会话对象,Session相当于1个微型浏览器,能够自动帮我们保持请求中的某些参数(如cookies),有了它,我们一般不需要额外去处理cookies、header等。
假如我们是在Fiddler打开的状态下,通过代码进行请求,那么将会遇到SSLError的错误,而当加上 self.s.verify = False 这行代码后,我们便可以忽略该错误。
requests.exceptions.SSLError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /login (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)')))
注意:
我们通过上面的代码忽略了SSLError的错误后,再次运行,这时仍然会出现2行警告,这2个警告并不影响我们的登录,可以不管它。
D:\Python\installation\lib\site-packages\urllib3\connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)
D:\Python\installation\lib\site-packages\urllib3\connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)
如果我们想去掉这2行警告,也可以通过如下代码来解决(针对Python3):
import urllib3
urllib3.disable_warnings()
2. 获取authenticity_token
login_url = "https://github.com/login"
r = self.s.get(login_url, headers = self.headers)
authenticity_token = re.findall('<input type="hidden" name="authenticity_token" value="(.+?)" />', r.text)
print("authenticity_token:{}".format(authenticity_token))
return authenticity_token[1]
当我们访问 https://github.com/login 时,登录界面会生成隐藏参数authenticity_token,而这恰是我们在登录提交表单时需要用到的参数。我们可通过正则表达式 re.findall 来获取authenticity_token。另外,我们还会发现,HTML界面中存在2个authenticity_token,因此通过正则返回的是一个长度为2的列表,经过分析,GitHub在登录时用到的是列表中的第二个元素,即authenticity_token[1]。
3. 模拟登录
def github_login(self, authenticity_token, username, password):
session_url = "https://github.com/session"
body = {
"authenticity_token":authenticity_token,
"commit":"Sign in",
"login":username,
"password":password,
"utf8":"✓",
"webauthn-support":"unknown"
}
r = self.s.post(session_url, headers = self.headers, data = body)
title = re.findall('<title>(.+?)</title>',r.text)
print("title:%s" %title[0])
return title[0]
我们在上面得到authenticity_token后,便可以来实现登录了。通过POST请求提交表单后,我们需要判断是否登录成功。在这里,我是通过页面的标题来判断GitHub是否登录成功,当然,还有许多方法可以用于判断。
4. 通过 title 判断是否登录成功
def is_login_success(self, title):
if "GitHub" == title:
return True
else:
return False
GitHub登录成功后,界面的标题会显示"GitHub",而登录失败时,一般显示的标题则是"Sign in to GitHub · GitHub"。
OK,以上就是通过Python模拟GitHub登录的过程,难度不大,相信大多数人阅读后都应该可以进行实践。
附源码:
import requests
import re
import urllib3
urllib3.disable_warnings()
class Github_Login():
def __init__(self):
# 设置Session
self.s = requests.session()
# 设置请求头
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:44.0) Gecko/20100101 Firefox/44.0"
}
# 在使用Fiddler时进行请求,通过该代码忽略SSLError错误
self.s.verify = False
# 获取 authenticity_token
def get_authenticity_token(self):
login_url = "https://github.com/login"
r = self.s.get(login_url, headers = self.headers)
authenticity_token = re.findall('<input type="hidden" name="authenticity_token" value="(.+?)" />', r.text)
print("authenticity_token:{}".format(authenticity_token))
return authenticity_token[1]
# 模拟登录,并返回 title
def github_login(self, authenticity_token, username, password):
session_url = "https://github.com/session"
body = {
"authenticity_token":authenticity_token,
"commit":"Sign in",
"login":username,
"password":password,
"utf8":"✓",
"webauthn-support":"unknown"
}
r = self.s.post(session_url, headers = self.headers, data = body)
title = re.findall('<title>(.+?)</title>',r.text)
print("title:%s" %title[0])
return title[0]
# 通过 title 判断是否登录成功
def is_login_success(self, title):
if "GitHub" == title:
return True
else:
return False
if __name__ == '__main__':
github = Github_Login()
authenticity_token = github.get_authenticity_token()
title = github.github_login(authenticity_token, username = "用户名", password = "密码")
login_result = github.is_login_success(title)
print(login_result)
如有错误,欢迎指出!
利用Python模拟GitHub登录的更多相关文章
- 利用Jmeter模拟Github登录
最近学习了Jmeter的简单操作,很想找点东西来实战一下,因为我之前写过一篇通过Python模拟登录的文章,于是便想尝试下学习通过Jmeter来模拟登录. 本人环境:Jmeter5.1.1 关于Git ...
- 忘记秘密利用python模拟登录暴力破解秘密
忘记秘密利用python模拟登录暴力破解秘密: #encoding=utf-8 import itertools import string import requests def gen_pwd_f ...
- 利用Python模拟登录pastebin.com
任务 在https://pastebin.com网站注册一个账号,利用python实现用户的自动登录和创建paste.该任务需要分成如下两步利用python实现: 账号的自动登录 paste的自动创建 ...
- python 模拟豆瓣登录(豆瓣6.0)
最近在学习python爬虫,看到网上有很多关于模拟豆瓣登录的例子,随意找了一个试了下,发现不能运行,对比了一下代码和豆瓣网站,发现原来是豆瓣网站做了修改,增加了反爬措施. 首先看下要模拟登录的网站: ...
- Python模拟接口登录
参考地址:https://blog.csdn.net/rifengxxc/article/details/77414090 下面讲下关于python模拟登录实验,之前怎么调试也不行,我也是摸索了好久, ...
- Python模拟校园网登录
最近忙着实验室的项目,学习的时间相对较少.前一段时间刚开始接触python时,依葫芦画瓢照着写了一个爬虫,爬取了某个网站的图片.当看到一张张图片自动出现在电脑屏幕上时,有些小小成就感.我想大多数人开始 ...
- python模拟自动登录网站(urllib2)
不登录打开网页: import urllib2 request = urllib2.Request('http://www.baidu.com') response = urllib2.urlopen ...
- 利用python模拟菜刀反弹shell绕过限制
有的时候我们在获取到目标电脑时候如果对方电脑又python 编译环境时可以利用python 反弹shell 主要用到python os库和sokect库 这里的服务端在目标机上运行 from sock ...
- 定向爬虫 - Python模拟新浪微博登录
当我们试图从新浪微博抓取数据时,我们会发现网页上提示未登录,无法查看其他用户的信息. 模拟登录是定向爬虫制作中一个必须克服的问题,只有这样才能爬取到更多的内容. 实现微博登录的方法有很多,一般我们在模 ...
随机推荐
- C# System.Timers.Timer的使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- Ogre 1.7.0,VS2005编译全过程傻瓜式教程
最近下了最新版Ogre 1.7.0,从下载到最后编译运行成功Ogre自带的Sample花了将近一下午时间. 网上有很多编译Ogre的教程,这里整理我看过的教程,加上自己的经验再详细总结一遍. 第一步: ...
- 【C#】wpf添加gif动图支持
原文:[C#]wpf添加gif动图支持 1.nuget里下载XamlAnimatedGif包,然后安装. 2.添加XamlAnimatedGif包的命名空间:xmlns:gif="https ...
- JavaScript eval() 函数,计算某个字符串,并执行其中的的 JavaScript 代码。
JavaScript eval() 函数,计算某个字符串,并执行其中的的 JavaScript 代码. 适合用于计算器的计算,等. 例子: eval("x=10;y=20;document. ...
- liunx 桥接 上网 ip配置 外部网络访问
一.设置VMware 在vmware的[编辑]-->[虚拟网络编辑器]设置:将VMnet0设置为“桥接”,并桥接到宿主机器的网卡(可以是有线或者无线网络). 二.设置虚拟机系统(以cento ...
- Windows 10 (IIS 10)安装Microsoft Web Farm Framework Version 2.2 for IIS7问题
But I got an error message "iis version 7.0 or greater is required to install Web Farm Framewor ...
- 推荐一个第三方Qt库的集合 good
https://inqlude.org/ Stable libraries | Development versions | Unreleased | Commercial | All attica ...
- Qt通过HTTP POST上传文件(python做服务端,附下载)
本文使用Qt Creator用HTTP POST的方法上传文件,并给出一个上传文件的例程. 本文主要客户端,所以对于服务器端程序编写的描述会比较简略 服务器使用Django编写,django服务器接收 ...
- 枚举当前系统用户(使用NetUserEnum API枚举)
using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unico ...
- 为什么Python中“2==2>1”结果为True
在Python中,你可能会发现这样一个奇怪的现象: >>> 2 == 2 > 1 True >>> (2 == 2) > 1 False >> ...