python - 如何使用通过证书的python请求登录cgi?
问题描述
我正在尝试使用 Python 中的请求模块来处理 cgi-bin 以登录网站,但我必须使用数字证书文件 .pfx 登录
我正在使用我找到的这段代码https://gist.github.com/erikbern/756b1d8df2d1487497d29b90e81f8068
@contextlib.contextmanager
def pfx_to_pem(pfx_path, pfx_password):
''' Decrypts the .pfx file to be used with requests. '''
with tempfile.NamedTemporaryFile(suffix='.pem') as t_pem:
f_pem = open(t_pem.name, 'wb')
pfx = open(pfx_path, 'rb').read()
p12 = OpenSSL.crypto.load_pkcs12(pfx, pfx_password)
f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate()))
ca = p12.get_ca_certificates()
if ca is not None:
for cert in ca:
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))
f_pem.close()
yield t_pem.name
with pfx_to_pem('path/file.pfx', 'password') as cert:
login_page = "https://zeusr.sii.cl/AUT2000/InicioAutenticacion/IngresoCertificado.html?https://misiir.sii.cl/cgi_misii/siihome.cgi"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "es-ES,es;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Cookie": "s_cc=true",
"Host": "herculesr.sii.cl",
"Origin": "https://zeusr.sii.cl",
"Referer": "https://misiir.sii.cl/cgi_misii/siihome.cgi",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-site",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
}
s = requests.Session()
s.cert = cert
print(s.cert)
r = s.get(login_page, cert=s.cert, headers=headers)
print(r.content)
当我发送带有我收到的标题
b'Error 400'
并且没有标题时,我收到了带有我未记录的消息的 html 文档
解决方案
好吧,最后我必须将 .pfx 文件转换为证书和密钥,并且可以轻松工作,只是在请求中我必须添加 verify=false
s = requests.Session()
s.cert = cert
r = s.get(login_page, cert=("certificate.cert", "certkey.key"), verify=false)
print(r.content)
推荐阅读
- c# - Razor 视图的性能分析
- git - Git:如何递归地添加文件夹中的所有文件
- python - 熊猫 groupby 和 value_counts
- python - Python 线程模块 - GUI 仍然冻结
- android - 使用 Retrofit 2.0.0 将多个文件发送到服务器
- javascript - 从两个 observable 访问数据,无需多个订阅
- powershell - Powershell 中的 Runspace Dispose(特定于 GUI)
- javascript - 带有 Intent 的 Alexa 调用名称不起作用
- python - tensorflow:在变量中找到较大的值并替换它
- c - 对联合数据的原始访问