Example #1
0
 def get_remote_request_cookiejar(self):
     from .models import ServerCookiejar
     server_cj, created = ServerCookiejar.objects.get_or_create(
         user=self.request.user)
     if created:
         from requests.utils import cookiejar_from_dict
         cookiejar = cookiejar_from_dict({})
     else:
         try:
             cookiejar = pickle.loads(server_cj.contents)
         except EOFError:
             from requests.utils import cookiejar_from_dict
             cookiejar = cookiejar_from_dict({})
     return cookiejar
Example #2
0
 def get_remote_request_cookiejar(self):
     from .models import ServerCookiejar
     server_cj, created = ServerCookiejar.objects.get_or_create(
         user=self.request.user)
     if created:
         from requests.utils import cookiejar_from_dict
         cookiejar = cookiejar_from_dict({})
     else:
         try:
             cookiejar = pickle.loads(server_cj.contents)
         except EOFError:
             from requests.utils import cookiejar_from_dict
             cookiejar = cookiejar_from_dict({})
     return cookiejar
Example #3
0
    def _on_request(self, adapter, request, **kwargs):
        match = self._find_match(request)
        # TODO(dcramer): find the correct class for this
        if match is None:
            error_msg = 'Connection refused: {0} {1}'.format(request.method,
                                                             request.url)
            response = ConnectionError(error_msg)
            response.request = request

            self._calls.add(request, response)
            raise response

        if 'body' in match and isinstance(match['body'], Exception):
            self._calls.add(request, match['body'])
            raise match['body']

        headers = {}
        if match['content_type'] is not None:
            headers['Content-Type'] = match['content_type']

        if 'callback' in match:  # use callback
            status, r_headers, body = match['callback'](request)
            if isinstance(body, six.text_type):
                body = body.encode('utf-8')
            body = BufferIO(body)
            headers.update(r_headers)

        elif 'body' in match:
            if match['adding_headers']:
                headers.update(match['adding_headers'])
            status = match['status']
            body = BufferIO(match['body'])

        response = HTTPResponse(
            status=status,
            reason=six.moves.http_client.responses[status],
            body=body,
            headers=headers,
            preload_content=False,
            # Need to not decode_content to mimic requests
            decode_content=False,
        )

        response = adapter.build_response(request, response)
        if not match.get('stream'):
            response.content  # NOQA

        try:
            resp_cookies = Cookies.from_request(response.headers['set-cookie'])
            response.cookies = cookiejar_from_dict(dict(
                (v.name, v.value)
                for _, v
                in resp_cookies.items()
            ))
        except (KeyError, TypeError):
            pass

        self._calls.add(request, response)

        return response
Example #4
0
    def login_auth_server(self):
        """ 登录至 AuthServer """
        # 如果之前的 cookie 已过期,则需要清理掉,否则会无限重定向
        self.session.cookies = cookiejar_from_dict({})
        login_html = self.session.get(
            'http://authserver.sdut.edu.cn/authserver/login')
        soup = BeautifulSoup(login_html.text, 'html.parser')
        ipts = soup.form.find_all('input')
        data = {
            'username': self.username,
            'password': self.password,
            # 'rememberMe': False
            'rememberMe': 'on',
        }
        for ipt in ipts:
            if ipt.get('value'):
                data[ipt.get('name')] = ipt.get('value')

        rst = self.session.post(
            'http://authserver.sdut.edu.cn/authserver/login', data=data)
        # 若页面跳转至首页,则说明登录成功
        if rst.url == 'http://authserver.sdut.edu.cn/authserver/index.do':
            return True
        # 若页面跳转回登录界面,则说明登录失败(用户名或密码错误)
        elif rst.url == 'http://authserver.sdut.edu.cn/authserver/login':
            return False
Example #5
0
    def update_cookie(self, username, password):
        _id = f't66y_{username}'
        if _id not in self.db or self.db[_id]['is_login']:
            result = self.login(username, password)
            if not self.is_ok(result):
                return result

            data = {
                'cookie': result['data']['cookie'],
                'is_login': False,
                '_id': _id,
                'update_time': self.now_time
            }
            self.db.create_document(data)

        document = self.db[_id]
        self.http.cookies = cookiejar_from_dict(document['cookie'])
        result = self.get_profile()
        if not self.is_ok(result):
            document['is_login'] = True
            document.save()
            return self.error(result['message'])

        if result['message'].find('俠客') != -1:
            self.send_tg('升级侠客啦')
            raise Exception('升级侠客啦')

        if self.reply_id not in document or len(
                document[self.reply_id]) <= 0 and self.day not in document:
            document[self.reply_id] = self.get_pending_reply_list()
            document['reply_count'] = random.randint(6, 10)
            document.save()

        return self.success(result['message'], {'document': document})
Example #6
0
	def __init__(self, basename='edapi', cookiefile=None):
		# Build common file names from basename.
		self._basename = basename
		if cookiefile:
			self._cookiefile = cookiefile
		else:
			self._cookiefile = self._basename + '.cookies'

		self._envfile = self._basename + '.vars'

		# Setup the HTTP session.
		self.opener = requests.Session()

		self.opener.headers = {
			'User-Agent': self._agent
		}

		# Read/create the cookie jar.
		if os.path.exists(self._cookiefile):
			try:
				with open(self._cookiefile, 'rb') as h:
					self.opener.cookies = cookiejar_from_dict(pickle.load(h))
			except:
				print('Cookie files exists, but is unreadable.')
		else:
			with open(self._cookiefile, 'wb') as h:
				pickle.dump(dict_from_cookiejar(self.opener.cookies), h)

		# Grab the commander profile
		self.response = self._getURI('profile')
 def http(self, path: Path) -> Path:
     session = Session()
     if self.cookies:
         session.cookies = cookiejar_from_dict(self.cookies)
     response = session.get(self.url)
     path.write_bytes(response.content)
     return path
Example #8
0
def cookies_load(session: requests.Session, username: str, path: str):
    data = {}
    if os.path.exists(path):
        with open(path, 'r') as fp:
            data = json.load(fp)
    if data and username in data:
        session.cookies.update(cookiejar_from_dict(data[username]))
Example #9
0
def _cookies_from_headers(headers):

    # Temporarily remove the comma from expires date strings
    date_safe = re.sub(r'expires=(...),',
                       r'expires=\1#',
                       headers['set-cookie'],
                       flags=re.IGNORECASE)

    # Split cookies by newline instead of commas
    line_split = "\n".join([c.strip() for c in date_safe.split(",")])

    # Restore date commas
    cookie_string = re.sub(r'expires=(...)#',
                           r'expires=\1,',
                           line_split,
                           flags=re.IGNORECASE)

    try:
        import http.cookies as cookies

        resp_cookie = cookies.SimpleCookie()
        resp_cookie.load(cookie_string)

        cookies_dict = {name: v.value for name, v in resp_cookie.items()}
    except ImportError:
        from cookies import Cookies

        resp_cookies = Cookies.from_response(cookie_string)
        cookies_dict = {v.name: v.value for _, v in resp_cookies.items()}
    return cookiejar_from_dict(cookies_dict)
 def get_cookie_twitter(self):
     try:
         if self.check_conf_file():
             cookiefileloaded = json.load(open(self._file_path))
             return cookiejar_from_dict(cookiefileloaded)
     except:
         return False
 def __cookies_load(self, username, path):
     data = {}
     if os.path.exists(path):
         with open(path, 'r') as fp:
             data = json.load(fp)
     if data and username in data:
         self.session.cookies.update(cookiejar_from_dict(data[username]))
Example #12
0
    def get_user_id_and_hidden_value(self, cookies=None):
        try:
            if cookies:
                self._session.cookies = cookiejar_from_dict(cookies)
            # We need to allow for redirects here as it performs 1-2 redirects before reaching the real profile url
            response = self._session.get('https://www.imdb.com/profile',
                                         allow_redirects=True)
        except RequestException as e:
            raise PluginError(str(e))

        user_id_match = re.search(r'ur\d+(?!\d)', response.url)
        if user_id_match:
            # extract the hidden form value that we need to do post requests later on
            try:
                soup = get_soup(response.text)
                self.hidden_value = soup.find('input',
                                              attrs={'id': '49e6c'})['value']
            except Exception as e:
                log.warning(
                    'Unable to locate the hidden form value '
                    '49e6c'
                    '. Without it, you might not be able to '
                    'add or remove items. %s',
                    e,
                )
        return user_id_match.group() if user_id_match else None
Example #13
0
    def load_session(self, filename: str) -> bytes:

        if exists(filename):
            with open(filename, '+r') as file:
                self.session.cookies = cookiejar_from_dict(json.load(file))
                response = self.session.get(urls.origin_url())
                status = self.status(response)
                return status
Example #14
0
    def load_cookies(self):
        '''加载cookies'''
        print("load_cookies()")

        if cf.has_option(self.config_section,self.config_cookies):
            ##string to dict
            d = eval(cf.get(self.config_section,self.config_cookies))
            self.session.cookies.update(cookiejar_from_dict(d))
Example #15
0
    def load_cookies(self):
        '''加载cookies'''
        print("load_cookies()")

        if cf.has_option(self.config_section, self.config_cookies):
            ##string to dict
            d = eval(cf.get(self.config_section, self.config_cookies))
            self.session.cookies.update(cookiejar_from_dict(d))
Example #16
0
    def _on_request(self, adapter, request, **kwargs):
        match = self._find_match(request)
        # TODO(dcramer): find the correct class for this
        if match is None:
            error_msg = 'Connection refused: {0} {1}'.format(request.method,
                                                             request.url)
            response = ConnectionError(error_msg)
            response.request = request

            self._calls.add(request, response)
            raise response

        if 'body' in match and isinstance(match['body'], Exception):
            self._calls.add(request, match['body'])
            raise match['body']

        headers = {}
        if match['content_type'] is not None:
            headers['Content-Type'] = match['content_type']

        if 'callback' in match:  # use callback
            status, r_headers, body = match['callback'](request)
            if isinstance(body, six.text_type):
                body = body.encode('utf-8')
            body = BufferIO(body)
            headers.update(r_headers)

        elif 'body' in match:
            if match['adding_headers']:
                headers.update(match['adding_headers'])
            status = match['status']
            body = BufferIO(match['body'])

        response = HTTPResponse(
            status=status,
            reason=six.moves.http_client.responses[status],
            body=body,
            headers=headers,
            preload_content=False,
        )

        response = adapter.build_response(request, response)
        if not match.get('stream'):
            response.content  # NOQA

        try:
            resp_cookies = Cookies.from_request(response.headers['set-cookie'])
            response.cookies = cookiejar_from_dict(dict(
                (v.name, v.value)
                for _, v
                in resp_cookies.items()
            ))
        except (KeyError, TypeError):
            pass

        self._calls.add(request, response)

        return response
Example #17
0
 def http(self, path: Path) -> Path:
     """Download the source file using HTTP (and cookies, if set at the
     initialization of the class)."""
     session = Session()
     if self.cookies:
         session.cookies = cookiejar_from_dict(self.cookies)
     response = session.get(self.url)
     path.write_bytes(response.content)
     return path
Example #18
0
 def __init__(self, ehall):
     if not isinstance(ehall, Ehall) or not ehall.logined:
         print("ehall 必须为已经登录的 Ehall 实例")
         return
     cookies = ehall.cookies
     self.ehall = ehall
     self.session = session()
     self.session.cookies = cookiejar_from_dict(json.loads(cookies))
     self.login()
Example #19
0
 def load_session_cookies(self):
     """
     loads & deserializes Cookie file if exists
     """
     if path.isfile(self.session_file):
         try:
             with open(self.session_file, 'rb') as handle:
                 _cookies = utils.cookiejar_from_dict(pickle.load(handle))
         except EOFError:
             _cookies = utils.cookiejar_from_dict({})
         except (ValueError, pickle.UnpicklingError):
             if self.settings.has_credentials():
                 USER, PASSWORD = self.settings.get_credentials()
             else:
                 USER, PASSWORD = self.settings.set_credentials()
             self.login(USER, PASSWORD, forceLogin=True)
         if hasattr(self, '_cookies'):
             self._session.cookies = _cookies
Example #20
0
 def __tryCookie(self):
     if self.__cookies != None:
         self.__session.cookies = self.__cookies
         response = self.__session.get(url=self.__tryCookieUrl, headers=self.__tryCookieHeader)
         if self.__tryCookieResponse in response.text:
             self.__cookieIsValid = True
             print("使用保存的Cookies登陆成功!")
         else:
             self.__session.cookies = cookiejar_from_dict({})
Example #21
0
 def __init__(self):
     self.session = Session()
     self.mailbox = str()
     self.cookie = dict()
     self.rear = str()
     self.address = list()
     self.message_status = list()
     self.message_box = list()
     self.session.cookies = cookiejar_from_dict(self.cookie)
     self.initial()
Example #22
0
def get_valid_cookies(cookies):
    def is_wp_cookie(key):
        return re.match(r'(wordpress|wp)(?!_*test)[A-z0-9]*', key,
                        re.IGNORECASE)

    valid_cookies = {
        key: value
        for key, value in cookies.items() if is_wp_cookie(key)
    }
    return cookiejar_from_dict(valid_cookies)
Example #23
0
 def test_task_aborts_when_response_has_no_valid_cookies(
         self, execute_task, monkeypatch):
     with pytest.raises(TaskAbort):
         invalid_cookies = {
             '__cfduid': '16a85284e4ee53f4933760b08b2bc82c',
             'wordpress_test_cookie': 'test+cookie',
         }
         _mock_session_response(
             mock.Mock(cookies=cookiejar_from_dict(invalid_cookies),
                       history=[]), monkeypatch)
         execute_task('test')
Example #24
0
    def load_session(self):
        """
        Generates the build up session object,
        loads & deserializes Cookie file if exists

        :returns:  requests.session -- Session object
        """
        _session = session()
        _session.headers.update({
            'User-Agent': self.utils.get_user_agent(),
            'Accept-Encoding': 'gzip'
        })
        if path.isfile(self.session_file):
            with open(self.session_file, 'r') as handle:
                try:
                    _cookies = utils.cookiejar_from_dict(pickle.load(handle))
                except EOFError:
                    _cookies = utils.cookiejar_from_dict({})
                _session.cookies = _cookies
        return _session
Example #25
0
    def get_user_id(self, cookies=None):
        try:
            if cookies:
                self._session.cookies = cookiejar_from_dict(cookies)
            # We need to allow for redirects here as it performs 1-2 redirects before reaching the real profile url
            response = self._session.head('https://www.imdb.com/profile', allow_redirects=True)
        except RequestException as e:
            raise PluginError(str(e))

        match = re.search('ur\d+(?!\d)', response.url)
        return match.group() if match else None
def _cookies_from_headers(headers):
    if sys.version_info[:2] < (3, 4):
        from cookies import Cookies

        resp_cookies = Cookies.from_request(headers["set-cookie"])
        cookies_dict = {v.name: v.value for _, v in resp_cookies.items()}
    else:
        import biscuits

        cookies_dict = biscuits.parse(headers["set-cookie"])
    return cookiejar_from_dict(cookies_dict)
Example #27
0
 def add_cookies(self, cookies):
     if isinstance(cookies, str):
         cookie_dict = {}
         for cookie_pair in cookies.split('; '):
             k, v = cookie_pair.split('=', 1)
             cookie_dict[k] = v
         cookies = cookie_dict
     if isinstance(cookies, dict):
         cookies = {k: self._transcode(v) for k, v in cookies.items()}
         cookies = cookiejar_from_dict(cookies)
     self._session.cookies.update(cookies)
Example #28
0
    def logout(self):
        """
        Unauthenticate from the client and destroy credential cache.
        """

        try:
            self.session.cookies = cookiejar_from_dict({})
            self.session.headers = default_headers()
            return True
        except Exception:  # pylint:disable=broad-except
            return False
Example #29
0
    def __init__(self, login, password, cookies=None):
        self._login = login
        self._password = password
        self._session = Session()
        self._cookies = cookies

        if cookies is not None:
            try:
                with open(cookies) as f:
                    self._session.cookies = cookiejar_from_dict(pickle_load(f))
            except IOError:
                _logger.error('Could not load cookies from {0}'.format(self._cookies))
Example #30
0
    def __init__(self, cookie, **kwargs):
        C = SimpleCookie()
        C.load(cookie)
        self.name = C['BUNSOURCE'].value

        cookies = {}
        [cookies.update({k: C[k].value}) for k in C]

        super().__init__(**kwargs)
        self.session.cookies = cookiejar_from_dict(cookies)
        if 'user-agent' in kwargs:
            self.session.headers.update({"user-agent": kwargs['user-agent']})
Example #31
0
    def load_user_cookies(self):
        if self._username is None:
            raise SettingsException(f'Empty username. You forgot to call `open_user`?')

        jar_string = self._get_user_key(self._username, 'cookies')

        if jar_string is None:
            return None

        jar_dict = json.loads(jar_string)

        return cookiejar_from_dict(jar_dict, self.cookie_jar_class())
Example #32
0
 def test_cookies_collected_across_redirects(self, execute_task,
                                             monkeypatch):
     valid_cookies = {
         'wordpress_logged_in_a32b6': '16a85284e4ee53f4933760b08b2bc82c',
         'wordpress_sec': 'test_value',
     }
     mock_history = [mock.Mock(cookies=cookiejar_from_dict(valid_cookies))]
     _mock_session_response(
         mock.Mock(cookies=RequestsCookieJar(), history=mock_history),
         monkeypatch)
     task = execute_task('test')
     assert len(
         task.requests.cookies) > 0, 'No cookies found within response'
Example #33
0
    def unqueue_cookies():
        """
        反序列化cookies

        :return:
        """
        try:
            with open(COOKIES_PATH, 'r', encoding='utf-8') as file:
                cookies_dict = json.load(file)
        except JSONDecodeError as e:
            raise e
        else:
            return cookiejar_from_dict(cookies_dict)
Example #34
0
 def login_cookies(self, cookies_dict):
     url = self.urlEAS + 'home.action'
     self.r.cookies = cookiejar_from_dict(cookies_dict,
                                          cookiejar=None,
                                          overwrite=True)
     login_r = self.r.get(url, headers=self.headers)
     result = login_r.text
     AC = re.findall('href="/eams/security/my.action"', result)
     if AC:
         print('cookie login success')
         return json.dumps(cookies_dict)
     else:
         return False
Example #35
0
def _cookies_from_headers(headers):
    try:
        import http.cookies as cookies

        resp_cookie = cookies.SimpleCookie()
        resp_cookie.load(headers["set-cookie"])

        cookies_dict = {name: v.value for name, v in resp_cookie.items()}
    except ImportError:
        from cookies import Cookies

        resp_cookies = Cookies.from_request(headers["set-cookie"])
        cookies_dict = {v.name: v.value for _, v in resp_cookies.items()}
    return cookiejar_from_dict(cookies_dict)
Example #36
0
	def login(self, data, url=None):
		if re.match("^1{0-9}{10}$", data["account"]):
			account_type = "phone_num"
			url_login = url or "https://www.zhihu.com/login/phone_num"
		elif re.match(".+@.+\.com", data["account"]):
			account_type = "email"
			url_login = url or "https://www.zhihu.com/login/email"
		else:
			print("账号类型错误")

		self._data = {
			"_xsrf": self._session.cookies.get("_xsrf", ""),
			"password": data.get("password", ""),
			"captcha": self._captcha,
			"remember_me": "true",
			account_type: data.get("account", "")
		}
		self._headers["X-Xsrftoken"] = self._session.cookies.get("_xsrf", "")
		self._r = self._session.post(url_login, data=self._data, headers=self._headers)
		if self._r.status_code != 200:
			print("提交数据失败")
		else:
			self._response_json = json.loads(self._r.content.decode("utf-8"))
			if self._response_json["r"] == 0:
				print(self._response_json["msg"])
				# save cookies
				lwpcookie = LWPCookieJar('cookie.txt')
				cookiejar_from_dict({ c.name: c.value for c in self._session.cookies}, lwpcookie)
				lwpcookie.save(ignore_discard=True)
			else:
				if self._response_json["errcode"] in [1991829, 100005]:
					print(self._response_json["msg"])
					self.get_captcha()
					self.login()
				else:
					print("未知的错误")
    def __init__(self, basename='edapi', debug=False, cookiefile=None):
        '''
        Initialize
        '''

        # Build common file names from basename.
        self._basename = basename
        if cookiefile:
            self._cookiefile = cookiefile
        else:
            self._cookiefile = self._basename + '.cookies'

        self._envfile = self._basename + '.vars'

        self.debug = debug
        # if self.debug:
        #     import http.client
        #     http.client.HTTPConnection.debuglevel = 3

        # Setup the HTTP session.
        self.opener = requests.Session()

        self.opener.headers = {
            'User-Agent': self._agent
        }

        # Read/create the cookie jar.
        if os.path.exists(self._cookiefile):
            try:
                with open(self._cookiefile, 'rb') as h:
                    self.opener.cookies = cookiejar_from_dict(pickle.load(h))
            except:
                print('Unable to read cookie file.')

        else:
            with open(self._cookiefile, 'wb') as h:
                pickle.dump(dict_from_cookiejar(self.opener.cookies), h)

        # Grab the commander profile
        response = self._getURI('profile')
        try:
            self.profile = response.json()
        except:
            sys.exit('Unable to parse JSON response for /profile!\
                     Try with --debug and report this.')
Example #38
0
    def _on_request(self, adapter, request, **kwargs):
        match = self._find_match(request)
        # TODO(dcramer): find the correct class for this
        if match is None:
            error_msg = "Connection refused: {0} {1}".format(request.method, request.url)
            response = ConnectionError(error_msg)

            self._calls.add(request, response)
            raise response

        if "body" in match and isinstance(match["body"], Exception):
            self._calls.add(request, match["body"])
            raise match["body"]

        headers = {"Content-Type": match["content_type"]}

        if "callback" in match:  # use callback
            status, r_headers, body = match["callback"](request)
            if isinstance(body, six.text_type):
                body = body.encode("utf-8")
            body = BufferIO(body)
            headers.update(r_headers)

        elif "body" in match:
            if match["adding_headers"]:
                headers.update(match["adding_headers"])
            status = match["status"]
            body = BufferIO(match["body"])

        response = HTTPResponse(status=status, body=body, headers=headers, preload_content=False)

        response = adapter.build_response(request, response)
        if not match.get("stream"):
            response.content  # NOQA

        try:
            resp_cookies = Cookies.from_request(response.headers["set-cookie"])
            response.cookies = cookiejar_from_dict(dict((v.name, v.value) for _, v in resp_cookies.items()))
        except (KeyError, TypeError):
            pass

        self._calls.add(request, response)

        return response
Example #39
0
    def get_user_id_and_hidden_value(self, cookies=None):
        try:
            if cookies:
                self._session.cookies = cookiejar_from_dict(cookies)
            # We need to allow for redirects here as it performs 1-2 redirects before reaching the real profile url
            response = self._session.get('https://www.imdb.com/profile', allow_redirects=True)
        except RequestException as e:
            raise PluginError(str(e))

        user_id_match = re.search('ur\d+(?!\d)', response.url)
        if user_id_match:
            # extract the hidden form value that we need to do post requests later on
            try:
                soup = get_soup(response.text)
                self.hidden_value = soup.find('input', attrs={'id': '49e6c'})['value']
            except Exception as e:
                log.warning('Unable to locate the hidden form value ''49e6c''. Without it, you might not be able to '
                            'add or remove items. %s', e)
        return user_id_match.group() if user_id_match else None
Example #40
0
 def authentication(self, username=None, force=False):
     """ Authentication """
     if path.isfile(self.cookie_jar_path) and not force:
         with open(self.cookie_jar_path) as cookie_jar_file:
             cookies = cookiejar_from_dict(load(cookie_jar_file))
             timestamp_now = mktime(datetime.utcnow().timetuple())
             if int(cookies['token_expire']) > timestamp_now:
                 self.profile['session'].cookies = cookies
             else:
                 force = True
     if not path.isfile(self.cookie_jar_path) or force:
         # Thank you, python2-3 team, for making such a fantastic mess with
         # input/raw_input :-)
         real_raw_input = vars(__builtins__).get('raw_input', input)
         if username is None:
             self.profile['username'] = real_raw_input('Username: '******'username'] = username
         self.profile['password'] = getpass()
         self.cookie_gen()
Example #41
0
 def login(self, session=None):
     if not session:
         print('在选课页面中刷新并复制Request Headers的cookie')
         cookie = input('Give me cookie [xx=xx; ...]: ')
     else:
         cookie = 'JSESSIONID=%s' % session
     q = {k:v for k,v in re.findall(r'([^=]*)=([^;]*);{0,1}\s{0,1}', cookie)}
     self.r.cookies = cookiejar_from_dict(q)
     print(q)
     test = self.r.get('http://eams.uestc.edu.cn/eams/stdElectCourse.action').text
     if '进入选课>>>>' in test:
         return True
     elif '未到选课时间' in test:
         log('未到选课时间')
         exit()
         return False
     else:
         log('Error login')
         exit()
         return False
 def test_task_aborts_when_response_has_no_valid_cookies(self, execute_task, monkeypatch):
     with pytest.raises(TaskAbort):
         invalid_cookies = {'__cfduid': '16a85284e4ee53f4933760b08b2bc82c', 'wordpress_test_cookie': 'test+cookie'}
         _mock_session_response(mock.Mock(cookies=cookiejar_from_dict(invalid_cookies), history=[]), monkeypatch)
         execute_task('test')
 def test_cookies_collected_across_redirects(self, execute_task, monkeypatch):
     valid_cookies = {'wordpress_logged_in_a32b6': '16a85284e4ee53f4933760b08b2bc82c', 'wordpress_sec': 'test_value'}
     mock_history = [mock.Mock(cookies=cookiejar_from_dict(valid_cookies))]
     _mock_session_response(mock.Mock(cookies=RequestsCookieJar(), history=mock_history), monkeypatch)
     task = execute_task('test')
     assert len(task.requests.cookies) > 0, 'No cookies found within response'
Example #44
0
    def _on_request(self, session, request, **kwargs):
        match = self._find_match(request)
        # TODO(dcramer): find the correct class for this
        if match is None:
            error_msg = 'Connection refused: {0} {1}'.format(request.method,
                                                             request.url)
            response = ConnectionError(error_msg)

            self._calls.add(request, response)
            raise response

        if 'body' in match and isinstance(match['body'], Exception):
            self._calls.add(request, match['body'])
            raise match['body']

        headers = {
            'Content-Type': match['content_type'],
        }

        if 'callback' in match:  # use callback
            status, r_headers, body = match['callback'](request)
            if isinstance(body, six.text_type):
                body = body.encode('utf-8')
            body = BufferIO(body)
            headers.update(r_headers)

        elif 'body' in match:
            if match['adding_headers']:
                headers.update(match['adding_headers'])
            status = match['status']
            body = BufferIO(match['body'])

        response = HTTPResponse(
            status=status,
            body=body,
            headers=headers,
            preload_content=False,
        )

        adapter = session.get_adapter(request.url)

        response = adapter.build_response(request, response)
        if not match.get('stream'):
            response.content  # NOQA

        try:
            resp_cookies = Cookies.from_request(response.headers['set-cookie'])
            response.cookies = cookiejar_from_dict(dict(
                (v.name, v.value)
                for _, v
                in resp_cookies.items()
            ))
            session.cookies = response.cookies
        except (KeyError, TypeError):
            pass

        self._calls.add(request, response)

        if kwargs.get('allow_redirects') and response.is_redirect:
            # include redirect resolving logic from requests.sessions.Session
            keep_kws = ('stream', 'timeout', 'cert', 'proxies')
            resolve_kwargs = dict([(k, v) for (k, v) in kwargs.items() if
                                   k in keep_kws])
            # this recurses if response.is_redirect,
            # but limited by session.max_redirects
            gen = session.resolve_redirects(response, request,
                                            **resolve_kwargs)
            history = [resp for resp in gen]

            # Shuffle things around if there's history.
            if history:
                # Insert the first (original) request at the start
                history.insert(0, response)
                # Get the last request made
                response = history.pop()
                response.history = history

        return response
Example #45
0
 def set_cookies(self, cookies):
     # Session.cookies should be class of CookieJar instead of dict
     self.sess.cookies = cookiejar_from_dict(cookies)
     logger.info('Session new cookies %s', self.sess.cookies)
Example #46
0
def get_valid_cookies(cookies):
    def is_wp_cookie(key):
        return re.match(r'(wordpress|wp)(?!_*test)[A-z0-9]*', key, re.IGNORECASE)

    valid_cookies = {key: value for key, value in cookies.items() if is_wp_cookie(key)}
    return cookiejar_from_dict(valid_cookies)
Example #47
0
def collect_cookies(response):
    cookies = dict_from_cookiejar(response.cookies)
    for h_resp in response.history:
        cookies.update(dict_from_cookiejar(h_resp.cookies))
    return cookiejar_from_dict(cookies)