在创建代理时可能会发生失败
proxy = server.create_proxy()
异常信息:Exception(u'Could not read Browsermob-Proxy json\nAnother server running on this port?\njava.lang.RuntimeException: java.net.BindException: \u5730\u5740\u5df2\u5728\u4f7f\u7528\n\tat org.littleshoot.proxy.impl.DefaultHttpProxyServer.doStart(DefaultHttpProxyServer.java:545)\n\tat org.littleshoot.proxy.impl.DefaultHttpProxyServer.start(DefaultHttpProxyServer.java:489)\n\tat org.littleshoot.proxy.impl.DefaultHttpProxyServer.access$700(DefaultHttpProxyServer.java:69)\n\tat org.littleshoot.proxy.impl.DefaultHttpProxyServer$DefaultHttpProxyServerBootstrap.start(DefaultHttpProxyServer.java:858)\n\tat net.lightbody.bmp.BrowserMobProxyServer...',)
这个代理是一个Client对象,
class Client(object):
def __init__(self, url, params=None, options=None):
"""
Initialises a new Client object
:param url: This is where the BrowserMob Proxy lives
:param params: URL query (for example httpProxy and httpsProxy vars)
:param options: Dictionary that can contain the port of an existing
proxy to use (for example 'existing_proxy_port_to_use')
"""
params = params if params is not None else {}
options = options if options is not None else {}
self.host = "http://" + url
if params:
urlparams = "?" + unquote(urlencode(params))
else:
urlparams = ""
if 'existing_proxy_port_to_use' in options:
self.port = options['existing_proxy_port_to_use']
else:
resp = requests.post('%s/proxy' % self.host + urlparams)
content = resp.content.decode('utf-8')
try:
jcontent = json.loads(content)
except Exception as e:
raise Exception("Could not read Browsermob-Proxy json\n"
"Another server running on this port?\n%s..." % content[:512])
self.port = jcontent['port']
url_parts = self.host.split(":")
self.proxy = url_parts[1][2:] + ":" + str(self.port)
创建代理时, 向server发送post请求, 正常情况下返回一个json字符串, 其中包含代理端口。 失败的情况下,返回异常信息。
但是, 在失败的时候, 查看server进程的文件描述符, 发现会产生一些pipe、anon_inode:[eventpoll]等文件。
刚启动时:
成功执行1次:
发生3次创建失败: