diff --git a/DrissionPage/_configs/session_options.py b/DrissionPage/_configs/session_options.py index d918f63..ed8f808 100644 --- a/DrissionPage/_configs/session_options.py +++ b/DrissionPage/_configs/session_options.py @@ -22,6 +22,9 @@ class SessionOptions(object): """ self.ini_path = None self._download_path = '' + self._timeout = 10 + self._del_set = set() # 记录要从ini文件删除的参数 + self._headers = None self._cookies = None self._auth = None @@ -34,46 +37,45 @@ class SessionOptions(object): self._stream = None self._trust_env = None self._max_redirects = None - self._timeout = 10 - self._del_set = set() # 记录要从ini文件删除的参数 + if read_file is False: + return - if read_file is not False: - ini_path = str(ini_path) if ini_path else None - om = OptionsManager(ini_path) - self.ini_path = om.ini_path - options_dict = om.session_options + ini_path = str(ini_path) if ini_path else None + om = OptionsManager(ini_path) + self.ini_path = om.ini_path + options_dict = om.session_options - if options_dict.get('headers', None) is not None: - self.set_headers(options_dict['headers']) + if options_dict.get('headers', None) is not None: + self.set_headers(options_dict['headers']) - if options_dict.get('cookies', None) is not None: - self.set_cookies(options_dict['cookies']) + if options_dict.get('cookies', None) is not None: + self.set_cookies(options_dict['cookies']) - if options_dict.get('auth', None) is not None: - self._auth = options_dict['auth'] + if options_dict.get('auth', None) is not None: + self._auth = options_dict['auth'] - if options_dict.get('params', None) is not None: - self._params = options_dict['params'] + if options_dict.get('params', None) is not None: + self._params = options_dict['params'] - if options_dict.get('verify', None) is not None: - self._verify = options_dict['verify'] + if options_dict.get('verify', None) is not None: + self._verify = options_dict['verify'] - if options_dict.get('cert', None) is not None: - self._cert = options_dict['cert'] + if options_dict.get('cert', None) is not None: + self._cert = options_dict['cert'] - if options_dict.get('stream', None) is not None: - self._stream = options_dict['stream'] + if options_dict.get('stream', None) is not None: + self._stream = options_dict['stream'] - if options_dict.get('trust_env', None) is not None: - self._trust_env = options_dict['trust_env'] + if options_dict.get('trust_env', None) is not None: + self._trust_env = options_dict['trust_env'] - if options_dict.get('max_redirects', None) is not None: - self._max_redirects = options_dict['max_redirects'] + if options_dict.get('max_redirects', None) is not None: + self._max_redirects = options_dict['max_redirects'] - self.set_proxies(om.proxies.get('http', None), om.proxies.get('https', None)) - self._timeout = om.timeouts.get('implicit', 10) - self._download_path = om.paths.get('download_path', '') + self.set_proxies(om.proxies.get('http', None), om.proxies.get('https', None)) + self._timeout = om.timeouts.get('implicit', 10) + self._download_path = om.paths.get('download_path', '') # ===========须独立处理的项开始============ @property @@ -397,6 +399,25 @@ class SessionOptions(object): return s + def from_session(self, session): + """从Session对象中读取配置 + :param session: Session对象 + :return: 当前对象 + """ + self._headers = session.headers + self._cookies = session.cookies + self._auth = session.auth + self._proxies = session.proxies + self._hooks = session.hooks + self._params = session.params + self._verify = session.verify + self._cert = session.cert + self._adapters = session.adapters + self._stream = session.stream + self._trust_env = session.trust_env + self._max_redirects = session.max_redirects + return self + def session_options_to_dict(options): """把session配置对象转换为字典 diff --git a/DrissionPage/_configs/session_options.pyi b/DrissionPage/_configs/session_options.pyi index 3c0ae72..9caf336 100644 --- a/DrissionPage/_configs/session_options.pyi +++ b/DrissionPage/_configs/session_options.pyi @@ -115,5 +115,7 @@ class SessionOptions(object): def make_session(self) -> Session: ... + def from_session(self, session: Session) -> SessionOptions: ... + def session_options_to_dict(options: Union[dict, SessionOptions, None]) -> Union[dict, None]: ... diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index 0e5a13d..55227b8 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -7,6 +7,7 @@ from copy import copy from .._base.base import BasePage from .._commons.web import set_session_cookies, set_browser_cookies +from .._configs.session_options import SessionOptions from .._pages.chromium_base import ChromiumBase from .._pages.session_page import SessionPage from .._units.setter import TabSetter, WebPageTabSetter @@ -67,7 +68,7 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): self._mode = 'd' self._has_driver = True self._has_session = True - super().__init__(session_or_options=copy(page.session)) + super().__init__(session_or_options=SessionOptions(read_file=False).from_session(copy(page.session))) super(SessionPage, self).__init__(page=page, tab_id=tab_id) def __call__(self, loc_or_str, timeout=None): diff --git a/DrissionPage/_pages/session_page.py b/DrissionPage/_pages/session_page.py index 5a92319..e135e3c 100644 --- a/DrissionPage/_pages/session_page.py +++ b/DrissionPage/_pages/session_page.py @@ -30,16 +30,15 @@ class SessionPage(BasePage): self._response = None self._session = None self._set = None - self._s_set_start_options(session_or_options, None) + self._s_set_start_options(session_or_options) self._s_set_runtime_settings() self._create_session() if timeout is not None: self.timeout = timeout - def _s_set_start_options(self, session_or_options, none): + def _s_set_start_options(self, session_or_options): """启动配置 :param session_or_options: Session、SessionOptions - :param none: 用于后代继承 :return: None """ if not session_or_options or isinstance(session_or_options, SessionOptions): diff --git a/DrissionPage/_pages/session_page.pyi b/DrissionPage/_pages/session_page.pyi index 22f6963..98d4e91 100644 --- a/DrissionPage/_pages/session_page.pyi +++ b/DrissionPage/_pages/session_page.pyi @@ -23,15 +23,13 @@ class SessionPage(BasePage): self._session_options: SessionOptions = ... self._url: str = ... self._response: Response = ... - # self._download_path: str = ... - # self._DownloadKit: DownloadKit = ... self._url_available: bool = ... self.timeout: float = ... self.retry_times: int = ... self.retry_interval: float = ... self._set: SessionPageSetter = ... - def _s_set_start_options(self, session_or_options, none) -> None: ... + def _s_set_start_options(self, session_or_options: Union[Session, SessionOptions]) -> None: ... def _s_set_runtime_settings(self) -> None: ... @@ -114,9 +112,6 @@ class SessionPage(BasePage): @property def set(self) -> SessionPageSetter: ... - # @property - # def download(self) -> DownloadKit: ... - def post(self, url: str, data: Union[dict, str, None] = ...,