mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
修复监听器设置method时的问题;修复Driver逻辑问题;pop()全部增加None
This commit is contained in:
parent
99dfaf91da
commit
f4e549a414
@ -71,26 +71,29 @@ class ChromiumDriver(object):
|
||||
try:
|
||||
self._ws.send(message_json)
|
||||
except OSError:
|
||||
self.method_results.pop(ws_id)
|
||||
self.method_results.pop(ws_id, None)
|
||||
return None
|
||||
|
||||
while not self._stopped.is_set():
|
||||
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:
|
||||
if self.alert_flag:
|
||||
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:
|
||||
return {'error': {'message': 'timeout'}}
|
||||
result = {'error': {'message': 'timeout'}}
|
||||
self.method_results.pop(ws_id, None)
|
||||
return result
|
||||
|
||||
continue
|
||||
|
||||
finally:
|
||||
self.method_results.pop(ws_id)
|
||||
|
||||
def _recv_loop(self):
|
||||
"""接收浏览器信息的守护线程方法"""
|
||||
while not self._stopped.is_set():
|
||||
|
@ -169,8 +169,8 @@ def cookie_to_dict(cookie):
|
||||
"""
|
||||
if isinstance(cookie, Cookie):
|
||||
cookie_dict = cookie.__dict__.copy()
|
||||
cookie_dict.pop('rfc2109')
|
||||
cookie_dict.pop('_rest')
|
||||
cookie_dict.pop('rfc2109', None)
|
||||
cookie_dict.pop('_rest', None)
|
||||
return cookie_dict
|
||||
|
||||
elif isinstance(cookie, dict):
|
||||
|
@ -209,7 +209,7 @@ class ChromiumOptions(object):
|
||||
:param arg: 设置项名称
|
||||
:return: 当前对象
|
||||
"""
|
||||
self._prefs.pop(arg)
|
||||
self._prefs.pop(arg, None)
|
||||
return self
|
||||
|
||||
def remove_pref_from_file(self, arg):
|
||||
|
@ -161,8 +161,7 @@ class SessionOptions(object):
|
||||
return self
|
||||
|
||||
attr = attr.lower()
|
||||
if attr in self._headers:
|
||||
self._headers.pop(attr)
|
||||
self._headers.pop(attr, None)
|
||||
|
||||
return self
|
||||
|
||||
|
@ -101,7 +101,7 @@ class BrowserDownloadManager(object):
|
||||
mission.final_path = final_path
|
||||
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._missions.pop(mission.id)
|
||||
self._missions.pop(mission.id, None)
|
||||
mission._is_done = True
|
||||
|
||||
def cancel(self, mission):
|
||||
@ -127,18 +127,9 @@ class BrowserDownloadManager(object):
|
||||
:param tab_id: 标签页id
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
self._tab_missions.pop(tab_id)
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
self._flags.pop(tab_id)
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
TabDownloadSettings.TABS.pop(tab_id)
|
||||
except KeyError:
|
||||
pass
|
||||
self._tab_missions.pop(tab_id, None)
|
||||
self._flags.pop(tab_id, None)
|
||||
TabDownloadSettings.TABS.pop(tab_id, None)
|
||||
|
||||
def _onDownloadWillBegin(self, **kwargs):
|
||||
"""用于获取弹出新标签页触发的下载任务"""
|
||||
|
@ -72,7 +72,7 @@ class NetworkListener(object):
|
||||
:param method: 设置监听的请求类型,可用list等指定多个,为None时监听全部
|
||||
:return: None
|
||||
"""
|
||||
if targets:
|
||||
if targets or method:
|
||||
self.set_targets(targets, is_regex, method)
|
||||
if self.listening:
|
||||
return
|
||||
@ -191,23 +191,26 @@ class NetworkListener(object):
|
||||
def _requestWillBeSent(self, **kwargs):
|
||||
"""接收到请求时的回调函数"""
|
||||
if not self._targets:
|
||||
rid = kwargs['requestId']
|
||||
p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, None))
|
||||
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']
|
||||
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))
|
||||
if not self._method or kwargs['request']['method'] in self._method:
|
||||
rid = kwargs['requestId']
|
||||
p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, None))
|
||||
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
|
||||
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):
|
||||
self._extra_info_ids.setdefault(kwargs['requestId'], {})['request'] = kwargs
|
||||
@ -244,11 +247,8 @@ class NetworkListener(object):
|
||||
|
||||
self._caught.put(dp)
|
||||
|
||||
try:
|
||||
self._request_ids.pop(r_id)
|
||||
self._extra_info_ids.pop(r_id)
|
||||
except:
|
||||
pass
|
||||
self._request_ids.pop(r_id, None)
|
||||
self._extra_info_ids.pop(r_id, None)
|
||||
|
||||
def _loading_failed(self, **kwargs):
|
||||
"""请求失败时的回调方法"""
|
||||
@ -263,11 +263,8 @@ class NetworkListener(object):
|
||||
dp._responseExtraInfo = ei.get('response', None)
|
||||
self._caught.put(dp)
|
||||
|
||||
try:
|
||||
self._request_ids.pop(r_id)
|
||||
self._extra_info_ids.pop(r_id)
|
||||
except:
|
||||
pass
|
||||
self._request_ids.pop(r_id, None)
|
||||
self._extra_info_ids.pop(r_id, None)
|
||||
|
||||
|
||||
class DataPacket(object):
|
||||
|
Loading…
x
Reference in New Issue
Block a user