Esempio n. 1
0
def get_or_create_session() -> requests.session:
    try:
        with open(SESSION_FILENAME, "rb") as session_dump:
            session = pickle.load(session_dump)
            log.debug("Session loaded for user")

    except FileNotFoundError:
        session = requests.session()

        # mount cookies and UA
        session.cookies.update({COOKIE_KEY: COOKIE_VALUE.strip()})
        session.headers["User-Agent"] = CHROME_UA

        # update cookies by visit website
        session.get("http://www.zhihu.com")

        validate_session(session)
        with open(SESSION_FILENAME, "wb") as session_dump:
            pickle.dump(session, session_dump)

    return session
Esempio n. 2
0
async def get_or_create_session() -> aiohttp.ClientSession:
    conn = gen_conn()
    try:
        with open(SESSION_FILENAME, "rb") as session_dump:
            cookies = pickle.load(session_dump)
            session = aiohttp.ClientSession(headers={"User-Agent": CHROME_UA},
                                            connector=conn, cookies=cookies)
            log.debug("Session loaded for user")

    except FileNotFoundError:
        # Aiohttp session
        # mount cookies and UA
        cookies = {COOKIE_KEY: COOKIE_VALUE}
        session = aiohttp.ClientSession(headers={"User-Agent": CHROME_UA},
                                        connector=conn, cookies=cookies)
        session._cookie_jar.update_cookies({COOKIE_KEY: COOKIE_VALUE.strip()})

        # update cookies by visit website
        await validate_session(session)
        with open(SESSION_FILENAME, "wb") as session_dump:
            pickle.dump(session.cookies, session_dump)

    return session