2016-11-15 52 views
3

如何使用python在PhantomJS中安裝SSL證書?如何在Python phantomjs中安裝SSL證書?

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

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

+0

你有沒有試過把你需要的cli命令作爲'service_arg'來傳遞? – jinksPadlock

+0

在你的問題中鏈接的問題有兩個很好的答案(那些更新)。什麼不工作? – Vaviloff

+0

@Vaviloff如果你仔細觀察鏈接的問題,你會發現它是關於phantomjs控制檯而不是Python的實現。我的問題是關於一個Python實現,正如我提到過兩次並進行了恰當的標記。 – User

回答

1

編譯回答«what is the correct way to feed an ssl certificate into phantomjs»«Is there a way to use PhantomJS in Python?»並考慮到你有沒有提到硒的考慮,我想你的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') 

另一種使用PhantomJS從Python是有幫助Selenium自動化工具。這樣,你就必須通過CLI還提供必要的certtificate文件:

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密鑰在PhantomJS從2.1版本的作品。

+0

哦,所以實際上沒有pythonic的方法,而只是從Python運行命令。 – User

+0

那麼請定義'pythonic way'。任何解決方案將「從python運行命令」,因爲PhantomJS不是一個python包,而是一個單獨的二進制執行文件。 – Vaviloff

+1

添加了Selenium示例。 – Vaviloff