2016-07-22 29 views
0

雖然我習慣了python,但我並不十分熟悉瀏覽器使用的所有協議。我不想爲我的硒webdriver設置代理,這是我使用的代碼。爲什麼browsermobproxy不能用於我的內部IP?

from browsermobproxy import Server, Client 
server = Server('/Users/***/Downloads/browsermob-proxy-2.1.1/bin/browsermob-proxy') 
server.start() 
proxy = server.create_proxy() 
profile = webdriver.FirefoxProfile() 
profile.set_proxy(proxy.selenium_proxy()) 
driver = webdriver.Firefox(firefox_profile=profile) 
proxy.new_har("10.203.9.156") 
driver.get("http://10.203.9.156") 
print json.dumps(proxy.har, indent =2) # returns a HAR JSON blob 
server.stop() 
driver.quit() 

我收到一個錯誤說

Unable to connect 

這將HAR從代理

{ 
"log": { 
"comment": "", 
"creator": { 
    "comment": "", 
    "version": "2.1.1", 
    "name": "BrowserMob Proxy" 
}, 
"version": "1.2", 
"entries": [ 
    { 
    "comment": "", 
    "serverIPAddress": "10.203.9.156", 
    "pageref": "10.203.9.156", 
    "startedDateTime": "2016-07-21T18:54:14.653-07:00", 
    "cache": {}, 
    "request": { 
     "comment": "", 
     "cookies": [], 
     "url": "http://10.203.9.156/", 
     "queryString": [], 
     "headers": [], 
     "headersSize": 317, 
     "bodySize": 0, 
     "method": "GET", 
     "httpVersion": "HTTP/1.1" 
    }, 
    "timings": { 
     "comment": "", 
     "receive": 0, 
     "send": 0, 
     "ssl": -1, 
     "connect": 7, 
     "dns": 0, 
     "blocked": 0, 
     "wait": 4 
    }, 
    "time": 12, 
    "response": { 
     "status": 301, 
     "comment": "", 
     "cookies": [], 
     "statusText": "Moved Permanently", 
     "content": { 
     "mimeType": "", 
     "comment": "", 
     "size": 0 
     }, 
     "headers": [], 
     "headersSize": 160, 
     "redirectURL": "https://10.203.9.156/login.html", 
     "bodySize": 0, 
     "httpVersion": "HTTP/1.1" 
    } 
    }, 
    { 
    "comment": "", 
    "serverIPAddress": "10.203.9.156", 
    "pageref": "10.203.9.156", 
    "startedDateTime": "2016-07-21T18:54:14.684-07:00", 
    "cache": {}, 
    "request": { 
     "comment": "", 
     "cookies": [], 
     "url": "https://10.203.9.156", 
     "queryString": [], 
     "headers": [], 
     "headersSize": 0, 
     "bodySize": 0, 
     "method": "CONNECT", 
     "httpVersion": "HTTP/1.1" 
    }, 
    "timings": { 
     "comment": "", 
     "receive": 0, 
     "send": 0, 
     "ssl": -1, 
     "connect": 193, 
     "dns": 0, 
     "blocked": 0, 
     "wait": 0 
    }, 
    "time": 194, 
    "response": { 
     "status": 0, 
     "comment": "", 
     "cookies": [], 
     "_error": "Unable to connect to host", 
     "statusText": "", 
     "content": { 
     "mimeType": "", 
     "comment": "", 
     "size": 0 
     }, 
     "headers": [], 
     "headersSize": -1, 
     "redirectURL": "", 
     "bodySize": -1, 
     "httpVersion": "unknown" 
    } 
    } 
], 
"pages": [ 
    { 
    "pageTimings": { 
     "comment": "" 
    }, 
    "comment": "", 
    "title": "10.203.9.156", 
    "id": "10.203.9.156", 
    "startedDateTime": "2016-07-21T18:54:14.602-07:00" 
    } 
], 
"browser": { 
    "comment": "", 
    "version": "46.0", 
    "name": "Firefox" 
} 
} 
} 

這是兩個答覆,我得到的IP和谷歌。

​​ google.com

有人可以解釋,這和如何的原因進行整治?

回答

0

最可能的是,由於https://10.203.9.156/login.html所提供的證書對於該IP地址無效,所以BMP拒絕與主機的連接。在嵌入模式和命令行/獨立/ REST API模式下,都會有一個trustAllServers選項來禁用證書檢查。我不確定Python包裝是否公開了這個選項;我建議諮詢Pyhton包裝的文檔,如果沒有,請提交PR。

相關問題