如何连接到要求我使用证书登录的intranet网页?

2024-04-18 19:29:20 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是我需要连接到一个intranet页面,该页面要求我输入外部证书(pendrive)的登录名和密码。你知道吗

我试过:

from bs4 import BeautifulSoup, SoupStrainer
import requests
import ssl
url = "https://intranet.website.i.need.to.connect"
page = requests.get(url, verify=True, cert='C:\\Users\\mmm\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\certifi\\cacert.pem')    
data = page.text
soup = BeautifulSoup(data)

for link in soup.find_all('a'):
    print(link.get('href'))

我还尝试下载证书点击挂锁(IE)并将其导出为.cer文件。我还尝试用notepad将enconding保存为UTF-8,并在上面代码中的cert=''参数处进行替换。你知道吗

我也试过这个:

import ssl
from requests import Session
from bs4 import BeautifulSoup as bs


server = 'https://intranet.website/'
#connection = httplib.HTTPSConnection(server, '443', timeout=60, 
 context=ssl.SSLContext(ssl.PROTOCOL_TLSv1))

with Session() as s:
    site = s.get('%s/login' % server, timeout=60, verify=False)
    bs_content = bs(site.content, "html.parser")
    token = bs_content.find("input", {"name":"csrf_token"})["value"]
    #login_data = {"username": "xxx", "password": "xxx"}
    s.post("%s/login" % server, login_data)
    home_page = s.get(server)
    print(home_page.content)

但没用

SSLError: HTTPSConnectionPool(host='intranet.website', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))

或者

Error: [('PEM routines', 'get_name', 'no start line'), ('SSL routines', 'SSL_CTX_use_PrivateKey_file', 'PEM lib')]

Tags: fromimporturlssldatagetbsserver