我已经度过了漫长而漫长的一天,试图获得与mechanize
或requests
一起使用的客户授权证书,并最终使用IEC
进行自我保护,这是幸运的简单 - 到现在为止。我只需要点击“是,使用此证书”,然后自动填写表单,找到链接并尝试下载文件。
我已经到了为第一个文件打开Open Save Cancel
对话框但无法看到如何与之交互的阶段。谷歌搜索我不确定如果不使用鼠标点击这似乎有点矫枉过正,我无法得到SendKeys.py
(link)
工作,因为我显然在误导vcvarsall.bat
。
今天一切似乎都是死路一条!
所以这里真的有两个问题。
1)有没有办法将cert.pem
和key.pem
个文件传递给requests
或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)或者有没有办法绕过或点击打开保存取消? (可接受的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中失败了吗?
答案 0 :(得分: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_certificate
API和Browser()
实例来指定客户端证书。