修复监听器设置method时的问题;修复Driver逻辑问题;pop()全部增加None

This commit is contained in:
g1879 2023-11-06 20:17:41 +08:00
parent 99dfaf91da
commit f4e549a414
6 changed files with 40 additions and 50 deletions

View File

@ -71,26 +71,29 @@ class ChromiumDriver(object):
try: try:
self._ws.send(message_json) self._ws.send(message_json)
except OSError: except OSError:
self.method_results.pop(ws_id) self.method_results.pop(ws_id, None)
return None return None
while not self._stopped.is_set(): while not self._stopped.is_set():
try: try:
return self.method_results[ws_id].get(timeout=.2) result = self.method_results[ws_id].get(timeout=.2)
self.method_results.pop(ws_id, None)
return result
except Empty: except Empty:
if self.alert_flag: if self.alert_flag:
self.alert_flag = False self.alert_flag = False
return {'result': {'message': 'alert exists.'}} result = {'result': {'message': 'alert exists.'}}
self.method_results.pop(ws_id, None)
return result
elif timeout is not None and perf_counter() > timeout: elif timeout is not None and perf_counter() > timeout:
return {'error': {'message': 'timeout'}} result = {'error': {'message': 'timeout'}}
self.method_results.pop(ws_id, None)
return result
continue continue
finally:
self.method_results.pop(ws_id)
def _recv_loop(self): def _recv_loop(self):
"""接收浏览器信息的守护线程方法""" """接收浏览器信息的守护线程方法"""
while not self._stopped.is_set(): while not self._stopped.is_set():

View File

@ -169,8 +169,8 @@ def cookie_to_dict(cookie):
""" """
if isinstance(cookie, Cookie): if isinstance(cookie, Cookie):
cookie_dict = cookie.__dict__.copy() cookie_dict = cookie.__dict__.copy()
cookie_dict.pop('rfc2109') cookie_dict.pop('rfc2109', None)
cookie_dict.pop('_rest') cookie_dict.pop('_rest', None)
return cookie_dict return cookie_dict
elif isinstance(cookie, dict): elif isinstance(cookie, dict):

View File

@ -209,7 +209,7 @@ class ChromiumOptions(object):
:param arg: 设置项名称 :param arg: 设置项名称
:return: 当前对象 :return: 当前对象
""" """
self._prefs.pop(arg) self._prefs.pop(arg, None)
return self return self
def remove_pref_from_file(self, arg): def remove_pref_from_file(self, arg):

View File

@ -161,8 +161,7 @@ class SessionOptions(object):
return self return self
attr = attr.lower() attr = attr.lower()
if attr in self._headers: self._headers.pop(attr, None)
self._headers.pop(attr)
return self return self

View File

@ -101,7 +101,7 @@ class BrowserDownloadManager(object):
mission.final_path = final_path mission.final_path = final_path
if mission.tab_id in self._tab_missions and mission.id in self._tab_missions[mission.tab_id]: if mission.tab_id in self._tab_missions and mission.id in self._tab_missions[mission.tab_id]:
self._tab_missions[mission.tab_id].remove(mission.id) self._tab_missions[mission.tab_id].remove(mission.id)
self._missions.pop(mission.id) self._missions.pop(mission.id, None)
mission._is_done = True mission._is_done = True
def cancel(self, mission): def cancel(self, mission):
@ -127,18 +127,9 @@ class BrowserDownloadManager(object):
:param tab_id: 标签页id :param tab_id: 标签页id
:return: None :return: None
""" """
try: self._tab_missions.pop(tab_id, None)
self._tab_missions.pop(tab_id) self._flags.pop(tab_id, None)
except KeyError: TabDownloadSettings.TABS.pop(tab_id, None)
pass
try:
self._flags.pop(tab_id)
except KeyError:
pass
try:
TabDownloadSettings.TABS.pop(tab_id)
except KeyError:
pass
def _onDownloadWillBegin(self, **kwargs): def _onDownloadWillBegin(self, **kwargs):
"""用于获取弹出新标签页触发的下载任务""" """用于获取弹出新标签页触发的下载任务"""

View File

@ -72,7 +72,7 @@ class NetworkListener(object):
:param method: 设置监听的请求类型可用list等指定多个为None时监听全部 :param method: 设置监听的请求类型可用list等指定多个为None时监听全部
:return: None :return: None
""" """
if targets: if targets or method:
self.set_targets(targets, is_regex, method) self.set_targets(targets, is_regex, method)
if self.listening: if self.listening:
return return
@ -191,23 +191,26 @@ class NetworkListener(object):
def _requestWillBeSent(self, **kwargs): def _requestWillBeSent(self, **kwargs):
"""接收到请求时的回调函数""" """接收到请求时的回调函数"""
if not self._targets: if not self._targets:
rid = kwargs['requestId'] if not self._method or kwargs['request']['method'] in self._method:
p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, None)) rid = kwargs['requestId']
p._raw_request = kwargs p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, None))
if kwargs['request'].get('hasPostData', None) and not kwargs['request'].get('postData', None):
p._raw_post_data = self._driver.call_method('Network.getRequestPostData', requestId=rid)['postData']
return
rid = kwargs['requestId']
for target in self._targets:
if ((self._is_regex and search(target, kwargs['request']['url'])) or
(not self._is_regex and target in kwargs['request']['url'])) and (
not self._method or kwargs['request']['method'] in self._method):
p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, target))
p._raw_request = kwargs p._raw_request = kwargs
if kwargs['request'].get('hasPostData', None) and not kwargs['request'].get('postData', None): if kwargs['request'].get('hasPostData', None) and not kwargs['request'].get('postData', None):
p._raw_post_data = self._driver.call_method('Network.getRequestPostData', requestId=rid)['postData'] p._raw_post_data = self._driver.call_method('Network.getRequestPostData', requestId=rid)['postData']
break return
else:
rid = kwargs['requestId']
for target in self._targets:
if ((self._is_regex and search(target, kwargs['request']['url'])) or
(not self._is_regex and target in kwargs['request']['url'])) and (
not self._method or kwargs['request']['method'] in self._method):
p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, target))
p._raw_request = kwargs
if kwargs['request'].get('hasPostData', None) and not kwargs['request'].get('postData', None):
p._raw_post_data = self._driver.call_method('Network.getRequestPostData', requestId=rid)[
'postData']
break
def _requestWillBeSentExtraInfo(self, **kwargs): def _requestWillBeSentExtraInfo(self, **kwargs):
self._extra_info_ids.setdefault(kwargs['requestId'], {})['request'] = kwargs self._extra_info_ids.setdefault(kwargs['requestId'], {})['request'] = kwargs
@ -244,11 +247,8 @@ class NetworkListener(object):
self._caught.put(dp) self._caught.put(dp)
try: self._request_ids.pop(r_id, None)
self._request_ids.pop(r_id) self._extra_info_ids.pop(r_id, None)
self._extra_info_ids.pop(r_id)
except:
pass
def _loading_failed(self, **kwargs): def _loading_failed(self, **kwargs):
"""请求失败时的回调方法""" """请求失败时的回调方法"""
@ -263,11 +263,8 @@ class NetworkListener(object):
dp._responseExtraInfo = ei.get('response', None) dp._responseExtraInfo = ei.get('response', None)
self._caught.put(dp) self._caught.put(dp)
try: self._request_ids.pop(r_id, None)
self._request_ids.pop(r_id) self._extra_info_ids.pop(r_id, None)
self._extra_info_ids.pop(r_id)
except:
pass
class DataPacket(object): class DataPacket(object):