1

我花了很长很长的时间试图获得一个客户端授权证书,或者mechanize最终requests求助于IEC自动化,这一直很简单——直到现在。我只需点击说“是的,使用此证书”,然后自动填写表格,找到链接并尝试下载文件。

我已经到了Open Save Cancel为第一个文件调出对话框的阶段,但看不到如何与之交互。谷歌搜索我不确定不使用鼠标点击是否可行,这似乎有点过分,我无法SendKeys.py (链接) 工作,因为我显然错过了vcvarsall.bat

今天的一切似乎都是死路一条!

所以这里真的有两个问题。

1)有没有办法将cert.pem文件key.pem传递给requestsor mechanize?(我的首选路线)

我目前的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')

最终超时时的回溯是:

Traceback (most recent call last):
  File "C:\Users\Jamie\Dropbox\workspace\XXXXX\XXXXX\myfile.py", line 29, in <module>
    r = br.open('https://www.example.com/secure/authorisedUsers.html')
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 203, in open
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 230, in _mech_open
  File "build\bdist.win32\egg\mechanize\_opener.py", line 193, in open
  File "build\bdist.win32\egg\mechanize\_urllib2_fork.py", line 344, in _open
  File "build\bdist.win32\egg\mechanize\_urllib2_fork.py", line 332, in _call_chain
  File "build\bdist.win32\egg\mechanize\_urllib2_fork.py", line 1170, in https_open
  File "build\bdist.win32\egg\mechanize\_urllib2_fork.py", line 1116, in do_open
  File "C:\Python27\Lib\httplib.py", line 1025, in getresponse
    response.begin()
  File "C:\Python27\Lib\httplib.py", line 401, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\Lib\httplib.py", line 365, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

我的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)或者有没有办法绕过或点击打开保存取消?(可接受的杂物)

编辑

我已经在 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 中失败了吗?

4

1 回答 1

1

以下是指定用于该 HTTPS 会话的客户端证书的方法:

机械化

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

要求

resp = requests.get('<https_url>', cert=(mycert, mykey), verify=False)

更新:我编辑了机械化代码。我们需要对实例使用add_client_certificateAPIBrowser()来指定客户端证书。

于 2016-09-30T00:52:54.870 回答