使用proxpy时如何避免SSL问题?

时间:2018-01-18 09:19:09

标签: python selenium ssl proxy

我正在尝试使用proxpy来监控python-selenium测试期间发出的任何请求。我使用的python代码如下:

from selenium import webdriver
PROXY = "0.0.0.0:9999"
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
    "httpProxy":PROXY,
    "ftpProxy":PROXY,
    "sslProxy":PROXY,
    "proxyType":"MANUAL"
}
driver = webdriver.Firefox()
driver.get("http://www.google.co.in")
driver.quit()

我按如下方式启动代理:

python proxpy/proxpy/proxpy.py -p 9999

当我运行python测试脚本时,我收到以下错误:

Exception happened during processing of request from ('127.0.0.1', 64667)
Traceback (most recent call last):
  File "/Users/adietz/anaconda/lib/python2.7/SocketServer.py", line 596, in process_request_thread
    self.finish_request(request, client_address)
  File "/Users/adietz/anaconda/lib/python2.7/SocketServer.py", line 331, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Users/adietz/Projects/Invest/browsermob/proxpy/proxpy/core.py", line 56, in __init__
    SocketServer.StreamRequestHandler.__init__(self, request, client_address, server)
  File "/Users/adietz/anaconda/lib/python2.7/SocketServer.py", line 652, in __init__
    self.handle()
  File "/Users/adietz/Projects/Invest/browsermob/proxpy/proxpy/core.py", line 137, in handle
    res = self.doCONNECT(host, port, req)
  File "/Users/adietz/Projects/Invest/browsermob/proxpy/proxpy/core.py", line 188, in doCONNECT
    ssl_version = ssl.PROTOCOL_SSLv23, do_handshake_on_connect = False)
  File "/Users/adietz/anaconda/lib/python2.7/ssl.py", line 933, in wrap_socket
    ciphers=ciphers)
  File "/Users/adietz/anaconda/lib/python2.7/ssl.py", line 544, in __init__
    self._context.load_cert_chain(certfile, keyfile)
IOError: [Errno 2] No such file or directory

我该如何解决这个问题?我怎样才能避免这个错误?我怎样才能监控请求和响应......?

在Linux机器上,错误是等效的:

Exception happened during processing of request from ('127.0.0.1', 35348)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 596, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/home/adietz/Projects/Invest/browsermob-proxy/proxpy/proxpy/core.py", line 56, in __init__
    SocketServer.StreamRequestHandler.__init__(self, request, client_address, server)
  File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
    self.handle()
  File "/home/adietz/Projects/Invest/browsermob-proxy/proxpy/proxpy/core.py", line 137, in handle
    res = self.doCONNECT(host, port, req)
  File "/home/adietz/Projects/Invest/browsermob-proxy/proxpy/proxpy/core.py", line 188, in doCONNECT
    ssl_version = ssl.PROTOCOL_SSLv23, do_handshake_on_connect = False)
  File "/usr/lib/python2.7/ssl.py", line 933, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 544, in __init__
    self._context.load_cert_chain(certfile, keyfile)
IOError: [Errno 2] No such file or directory

4 个答案:

答案 0 :(得分:1)

这看起来像是本身的问题。 我检查了Github页面,最后一次更新是从四年前开始的。 如果您能够切换代理我建议使用Browsermob,它非常用户友好。 由于你使用的是python我找到了一个着名的Selenium贡献者的包装库:https://github.com/AutomatedTester/browsermob-proxy-py

答案 1 :(得分:1)

我已设法解决问题中提到的proxpy的cert错误,但仍无法代理https请求。

如果您需要的是检查HTTP流量的代理,mitmproxy是一个方便的工具,也是最好的。

答案 2 :(得分:0)

我已经浏览了pproxy源代码库的代码。

@Alex如果您可以对代码进行一些更改。我认为它有效。

以下是我建议的更改。

core.py 在ProxyHandler类中,init方法更改对等初始化

来自

self.peer = False

self.peer = True

请通过创建目标应用程序的pem文件来解决证书问题。将它放在certs目录中。 {{这一点我不明白该怎么做。}}。请咨询@Homewrecker。

  

失败原因:

对等值用false进行编码。因此,每当它尝试创建一个httpConnection时,你会提供什么。

This Screenshot may help you navigate to the code

答案 3 :(得分:0)

你可以找到很多http proxy packages on the Cheese Shop。如果一个不起作用,只需选择另一个。

我正在随机使用现代Python支持pyproxy,它看起来像:

$ pyproxy --port=8888 --log-to-stderr
[I 180124 13:28:59 pyproxy:242] http server started on 127.0.0.1:8888
[I 180124 13:28:59 pyproxy:242] http server started on 127.0.0.1:8888
[I 180124 13:30:11 web:2064] 200 CONNECT www.google.co.in:443 (127.0.0.1) 223.28ms
[I 180124 13:30:11 web:2064] 200 CONNECT www.google.co.in:443 (127.0.0.1) 223.28ms

将客户请求作为:

curl -v --proxy http://localhost:8888 https://www.google.co.in/

同样如@georgexsh所述,您可以使用mitmproxy。请注意,它有一个Web GUI mitmweb,如下所示:

mitmweb