原创 

无密码登录 cookie 保活、有效性校验

分类:    280人阅读    IT小君  2019-05-02 19:22
import datetime
import json
import threading
import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

import app
from exts import db
from model.models import CookiePool
from selenium.webdriver.support.ui import WebDriverWait


class CookieService:
    def __init__(self):
        self.options = webdriver.FirefoxOptions()
       # self.options.add_argument('--headless')
        self.browser = webdriver.Firefox(executable_path=r'F:\Program Files (x86)\webdriver\geckodriver.exe',
                                         firefox_options=self.options)
        self.lock = threading.Lock()

    def test(self):
        print("running")
        self.keep_alive()

    def keep_alive(self):
        print('cookie 保活'+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        global currentid
        try:
            with app.app.app_context():
                if self.lock.acquire():
                    result = CookiePool.query.filter(
                        CookiePool.updateAt <= datetime.datetime.now() + datetime.timedelta(minutes=-5),
                        CookiePool.isValid.__eq__('Y')).order_by(
                        CookiePool.updateAt.desc()).all()

                    for cookies in result:
                        self.browser.get(cookies.rootUrl)
                        cookie = json.loads(cookies.cookie)
                        for item in cookie:
                            self.browser.add_cookie(item)
                        self.browser.get(cookies.validUrl)
                        currentid = cookies.id
                        WebDriverWait(self.browser, 50).until(
                            EC.presence_of_element_located(
                                (By.CSS_SELECTOR, cookies.validTarget)))
                        CookiePool.query.filter_by(id=currentid).update(
                            {'updateAt': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())})
        except Exception as ex:
            print(str(ex))
            with app.app.app_context():
                CookiePool.query.filter_by(id=currentid).update({'isValid': 'N'})
                db.session.commit()
            app.app.logger.error("cookie keep Alive exception"+str(currentid), exc_info=True)
        finally:
            self.lock.release()


点击广告,支持我们为你提供更好的服务

html5 canvas彩色碎片组合球形旋转动画特效

小众时尚单品在线电子商务网站模板

有机水果蔬菜HTML5网站模板

css+js实现的颜色渐变数字时钟动画特效

html5 svg夜空中星星流星动画场景特效

现代时尚家具公司网站模板

响应式咖啡饮品宣传网站模板

响应式太阳能能源公司网站模板

css鼠标跟随文字模糊特效

html5图标下拉搜索框自动匹配代码

中小型创意设计服务公司网站模板

响应式时尚单品在线商城网站模板

HTML5现代家居装潢公司网站模板

html5 canvas进度条圆环图表统计动画特效

网页设计开发公司网站模板

HTML5 Canvas竖直流动线条背景动画特效

js+css3抽奖转盘旋转点餐代码

HTML5数字产品服务公司网站模板

canvas炫酷鼠标移动文字粒子特效

jQuery右端悬浮带返回顶部特效

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务