如何在Python phantomjs中安装SSL证书?

时间:2016-11-15 01:45:27

标签: python ssl phantomjs

如何使用python在PhantomJS中安装SSL证书?

what is the correct way to feed an ssl certificate into phantomjs

在命令行中说,要使用--ssl-certificates-path

1 个答案:

答案 0 :(得分:1)

编译答案«what is the correct way to feed an ssl certificate into phantomjs»«Is there a way to use PhantomJS in Python?»并考虑到你没有提到Selenium,我想你的python脚本看起来与此类似:

command = "phantomjs --ignore-ssl-errors=true --ssl-client-certificate-file=C:\tmp\clientcert.cer --ssl-client-key-file=C:\tmp\clientcert.key --ssl-client-key-passphrase=1111 /path/to/phantomjs/script.js"

process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

# make sure phantomjs has time to download/process the page
# but if we get nothing after 30 sec, just move on
try:
    output, errors = process.communicate(timeout=30)
except Exception as e:
    print("\t\tException: %s" % e)
    process.kill()

# output will be weird, decode to utf-8 to save heartache
phantom_output = ''
for out_line in output.splitlines():
    phantom_output += out_line.decode('utf-8')

在Python中使用PhantomJS的另一种方法是借助Selenium自动化工具。这样,您还必须通过CLI提供必要的证明文件:

from selenium import webdriver

driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-client-certificate-file=C:\tmp\clientcert.cer', '--ssl-client-key-file=C:\tmp\clientcert.key', '--ssl-client-key-passphrase=1111'])
driver.set_window_size(1280, 1024)
driver.get('https://localhost/test/')
driver.save_screenshot('screen.png')
driver.quit()

请注意,提供自定义ssl键可在2.1版本的PhantomJS中使用。