向HTTPS站点Python发送客户端授权证书

2024-04-19 19:17:32 发布

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

我花了一整天的时间试图获得一个使用mechanizerequests的客户授权证书,最后求助于IEC来实现自动化,这一点到目前为止都非常简单。我只需要点击说“是的,使用这个证书”,然后自动填写表单,找到链接并尝试下载文件。在

我已经到了为第一个文件打开Open Save Cancel对话框的阶段,但是不知道如何与之交互。我不确定不使用鼠标点击是否有可能,这看起来有点过头了,我无法得到SendKeys.py(link) 我显然是错了vcvarsall.bat。在

今天一切似乎都是死胡同!在

所以这里有两个问题。在

1)有没有一种方法可以将cert.pemkey.pem文件传递给requests或{}?(我的首选路线)

我当前的mechanize代码是:

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.open(root_url + '/home.html')
# Hangs on the following line due to not passing Client Certificate
r = br.open(root_url + '/secure/secureTerms.html')

最终超时时的回溯是:

^{pr2}$

我的requests代码是:

    agent = requests.session()
    agent.get(root_url + '/home.html')
    data = {'redirectPage':'reportSearchAddressByPostcode',
            'accept':'Accept Terms'}
    agent.post(URL, data=data, headers=hdr)
    # Again, this hangs on the following line
    response = agent.get(root_url + '/secure/secureTerms.html', headers=hdr)

这是我从requests得到的回溯:

Traceback (most recent call last):
  File "C:\Users\Jamie\Dropbox\workspace\XXXXX\XXXXX\myfile.py", line 27, in <module>
    headers=hdr, cert=('cert.pem', 'key.pem'))
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 363, in get
    return self.request('GET', url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 347, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 460, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests\adapters.py", line 327, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.example.com', port=443): Max retries exceeded with url: /secure/authorisedUsers.html (Caused by <class 'httplib.BadStatusLine'>: '')

2)或者是否有方法绕过或单击“打开”“保存”“取消”?(可接受kludge)

编辑

我在cURL中尝试过这个方法,它可以很好地使用以下参数:

curl -k -v --key key.pem --cert cert.pem https://www.example.com/secure/authorisedUsers.html

服务器响应:

* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using DHE-RSA-AES256-SHA
* Server certificate:
*        subject: OU=Go to https://www.name.com/repository/index.html; OU=Name
 SSL123 certificate; OU=Domain Validated; CN=www.example.com
*        start date: 2012-12-03 00:00:00 GMT
*        expire date: 2013-12-03 23:59:59 GMT
*        issuer: C=US; O=Name, Inc.; OU=Domain Validated SSL; CN=Name DV SSL
 CA
*        SSL certificate verify result: unable to get local issuer certificate (
20), continuing anyway.
> GET /secure/authorisedUsers.html HTTP/1.1
> User-Agent: curl/7.30.0
> Host: www.example.com
> Accept: */*
>
* SSLv3, TLS handshake, Hello request (0):
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Request CERT (13):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS handshake, CERT verify (15):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
< HTTP/1.1 302 Moved Temporarily
< Date: Fri, 21 Jun 2013 00:52:44 GMT
< Set-Cookie: site-text-size=medium; Domain=www.example.com; Expires=Sat,
21-Jun-2014 00:52:44 GMT; Path=/
< Set-Cookie: user=SITE_MEMBER_V2; Domain=www.example.com; Path=/
< Location: https://www.example.com/secure/addressSearch.html
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
<
<html><body><p>Redirecting to <a href="https://www.example.com/secure/addr
essSearch.html">https://www.example.com/secure/addressSearch.html</a></p><
/body></html>* Closing connection 0
* SSLv3, TLS alert, Client hello (1):

这就是我所期望看到的,那么这里有什么可以说明它为什么在Python中失败的原因吗?在


Tags: keypycomclienthelloserverexamplehtml
1条回答
网友
1楼 · 发布于 2024-04-19 19:17:32

下面是如何指定要用于该HTTPS会话的客户端证书:

机械化

br = mechanize.Browser()
br.add_client_certificate(myurl, mykey, cert)
br.set_handle_robots( False )
#httplib.HTTPSConnection.connect = connect
resp = br.open(myurl)

请求

^{pr2}$

更新:我编辑了mechanize的代码。我们需要使用add_client_certificateAPI和Browser()实例来指定客户端证书。在

相关问题 更多 >