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

标签 python ssl phantomjs

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

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

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

最佳答案

编译答案«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()

请注意,从 2.1 版开始,提供自定义 ssl key 在 PhantomJS 中有效。

关于python - 如何在 Python phantomjs 中安装 SSL 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40600745/

相关文章:

python - Pandas 就地操作 DataFrame 与不就地操作(就地=True 与 False)

java - 移动 Java keystore

macos - 上传使用 Electron 开发并使用 electron-builder 构建的 macOS 应用程序时发生 CFBundleIdentifier 冲突

python - 给定均值和标准差生成二维正态分布

python - 如何在多标题/多索引 pandas Dataframe 中设置单元格值

python - 针对 NoSQL 键值对执行上下文搜索的正确方法是什么?

java - SSL 问题 java web 应用程序

ssl - 如何在 elasticsearch 5.5 中设置 server.name?

css - Phantomjs 中的 Flexbox 支持可在 html2ng node.js 中使用

python - 是否可以像 Selenium 中的移动驱动程序一样使用 PhantomJS?