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

3

你尝试过将所需的CLI命令作为“service_arg”传递吗? - user2096803
你的问题中链接的问题有两个很好的答案(较新的那些)。什么没有起作用? - Vaviloff
@Vaviloff 如果您仔细查看链接的问题,您会发现它是关于phantomjs控制台而不是Python实现的。我的问题是关于Python实现的,正如我两次提到并适当标记的那样。 - User
1个回答

1

编译答案“如何将ssl证书正确地输入PhantomJS”“有没有办法在Python中使用PhantomJS?”,考虑到您没有提到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')

使用Selenium自动化工具是另一种从Python中使用PhantomJS的方法。这种方式需要通过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()

请注意,从2.1版本开始,PhantomJS支持提供自定义SSL密钥。

哦,所以没有Pythonic的方法来实际完成它,而只能从Python中运行命令。 - User
请定义“pythonic way”。任何解决方案都将“从Python运行命令”,因为PhantomJS不是Python包,而是一个单独的可执行文件。 - Vaviloff
1
添加了Selenium示例。 - Vaviloff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接