0

虽然我习惯了 python,但我对浏览器使用的所有协议都不是很熟悉。我不想为我的 selenium webdriver 设置代理,这是我使用的代码。

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 得到的两个响应。

10.203.9.156 google.com

有人可以解释这个原因以及如何纠正这个问题吗?

4

1 回答 1

0

BMP 很可能拒绝与主机的连接,因为https://10.203.9.156/login.html提供的证书对该 IP 地址无效。在嵌入式模式和命令行/独立/REST API 模式下,都有一个trustAllServers选项可以禁用证书检查。我不确定 Python 包装器是否公开了该选项;我建议咨询 Pyhton 包装器的文档,如果没有,请提交 PR。

于 2016-07-23T20:07:33.410 回答