Ejemplo n.º 1
0
 def validUsefulProxy(params):
     """
     检验代理是否可用
     """
     marks = params.split('&&')
     if isinstance(marks[1], bytes):
         marks[1] = marks[1].decode('utf8')
     proxies = {"http": "http://{proxy}".format(proxy=marks[1])}
     flag = None
     try:
         # 超过20秒的代理就不要了
         r = requests.get('http://httpbin.org/ip',
                          proxies=proxies,
                          timeout=10,
                          verify=False)
         if r.status_code == 200 and r.json().get("origin"):
             # logger.info('%s is ok' % proxy)
             flag = True
     except Exception as e:
         flag = False
     if not flag:
         sqlite = Sqlite(address + 'ip.db')
         sqlite.update_data('delete from ip_house where id = {}'.format(
             marks[0]))
         Log.d("删除无效代理:" + marks[1])
Ejemplo n.º 2
0
 def validUsefulProxy(params):
     """
     检验代理是否可用
     """
     marks = params.split('&&')
     if isinstance(marks[1], bytes):
         marks[1] = marks[1].decode('utf8')
     if 'HTTP' == marks[2]:
         proxies = {"http": "http://{proxy}".format(proxy=marks[1])}
     else:
         proxies = {"https": "https://{proxy}".format(proxy=marks[1])}
     flag = None
     try:
         # 超过3秒的代理就不要了
         r = requests.get('http://httpbin.org/ip',
                          proxies=proxies,
                          timeout=3,
                          verify=False)
         if r.status_code == 200 and r.json().get("origin"):
             # print('请求IP:'+r.json().get("origin")+', 代理为:'+json.dumps(proxies))
             Log.v('有效代理:' + marks[1])
             flag = True
     except Exception as e:
         flag = False
     if not flag:
         sqlite = Sqlite(address + 'ip.db')
         sqlite.update_data('delete from ip_house where id = {}'.format(
             marks[0]))
         Log.d("无效代理:" + marks[1])
Ejemplo n.º 3
0
def main():

    GetFreeProxy.getAllProxy(THREAD_POOL_SIZE,THREAD_OR_PROCESS,IS_REFASH_IP_POOL)
    sqlite = Sqlite(address + 'ip.db')
    ips_results = sqlite.query_data('select proxy_adress from ip_house')

    login = Login()
    Log.v('正在登录...')
    result, msg = login.login(USER_NAME, USER_PWD, IS_AUTO_CHECK_CAPTHCA)
    if not Utils.check(result, msg):
        Log.e(msg)
        return
    Log.v('%s,登录成功' % msg)

    seatTypesCode = SEAT_TYPE_CODE if SEAT_TYPE_CODE else [SEAT_TYPE[key] for key in SEAT_TYPE.keys()]
    passengerTypeCode = PASSENGER_TYPE_CODE if PASSENGER_TYPE_CODE else '1'

    count = 0
    while True:
        # 死循环一直查票,直到下单成功
        try:
            nowTime, status = deadline.do_fix_time()
            if status:
                Log.v('当前时间:%s 处于23点到6点之间,12306处于维护状态,暂不处理下单业务' % nowTime)
                continue

            count += 1
            Log.v('第%d次访问12306网站' % count)
            print('-' * 40)
            ticketDetails = Query.loopQuery(TRAIN_DATE, FROM_STATION, TO_STATION,
                                            TrainUtils.passengerType2Desc(passengerTypeCode),
                                            TRAINS_NO,
                                            seatTypesCode, PASSENGERS_ID, POLICY_BILL, QUERY_TICKET_REFERSH_INTERVAL)
            Log.v('已为您查询到可用余票:%s' % ticketDetails)

            ticketDetails.passengersId = PASSENGERS_ID
            ticketDetails.ticketTypeCodes = passengerTypeCode
            ticketDetails.tourFlag = TOUR_FLAG if TOUR_FLAG else 'dc'
            submit = Submit(ticketDetails)
            if submit.submit(CHOOSE_SEATS):
                status, contents = submit.showSubmitInfoPretty()
                if status:
                    flag = send_mail(mail_user, mailto_list, '12306订票结果通知', mail_host, mail_user, mail_pass, contents)
                    if flag:
                        Log.v("邮件发送成功!")
                    else:
                        Log.v("邮件发送失败!")
                    sms_id = send_sms(ACCOUNT_SID,AUTO_TOKEN,FROM_NUM,TO_NUM,'小机机已经成功拿到小票票,请主人记得在30分钟内完成支付!!!')
                    if sms_id:
                        Log.v("短信提醒发送成功!")
                    else:
                        Log.v("短信提醒发送成功!")
                break
            time.sleep(1)
        except Exception as e:
            Log.w(e)
    print(login.loginOut())
Ejemplo n.º 4
0
def init_ip_pool():
    import os

    from utils.sqllite_handle import Sqlite
    address = os.path.dirname(os.path.dirname(
        os.path.realpath(__file__))) + '/'
    sqlite = Sqlite(address + 'ip.db')
    ips_results = sqlite.query_data('select proxy_adress from ip_house')
    for ip in ips_results:
        ips.append(ip[0])
Ejemplo n.º 5
0
def init_ip_pool():
    import os

    from utils.sqllite_handle import Sqlite
    address = os.path.dirname(os.path.dirname(
        os.path.realpath(__file__))) + '/'
    sqlite = Sqlite(address + 'ip.db')
    ips_results = sqlite.query_data('select proxy_adress,type from ip_house')
    Log.v('有效ip代理数量:' + str(len(ips_results)))
    for ip in ips_results:
        ips.append([ip[0], ip[1]])


# init_ip_pool()
Ejemplo n.º 6
0
    def getAllProxy(pool_size=10, thread_or_process=True, is_refash=True):

        Log.v('正在更新ip池,请稍后...')
        # address = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/'
        if is_refash:
            proxys = GetFreeProxy.get_list_proxys()
            proxys = list(set(proxys))
            sqlite = Sqlite(address + 'ip.db')
            sqlite.update_data('DELETE FROM ip_house')
            sqlite = Sqlite(address + 'ip.db')
            for i in range(len(proxys)):
                if proxys[i] and GetFreeProxy.verifyProxyFormat(proxys[i]):
                    sqlite.cursor.execute(
                        "INSERT INTO ip_house VALUES (?,?,?);",
                        [i + 1, proxys[i], 'true'])
            sqlite.conn.commit()
            sqlite.close_conn()
        else:
            sqlite = Sqlite(address + 'ip.db')
            results = sqlite.query_data(
                'select count(proxy_adress) from ip_house')
            if int(results[0][0]) == 0:
                proxys = GetFreeProxy.get_list_proxys()
                proxys = list(set(proxys))
                sqlite = Sqlite(address + 'ip.db')
                for i in range(len(proxys)):
                    if proxys[i] and GetFreeProxy.verifyProxyFormat(proxys[i]):
                        sqlite.cursor.execute(
                            "INSERT INTO ip_house VALUES (?,?,?);",
                            [i + 1, proxys[i], 'true'])
                sqlite.conn.commit()
                sqlite.close_conn()

        sqlite = Sqlite(address + 'ip.db')
        results = sqlite.query_data('select id,proxy_adress from ip_house')
        params = []
        for result in results:
            param = str(result[0]) + '&&' + result[1]
            params.append(param)
        Log.v("发现ip代理数量:" + str(len(params)))
        Log.v('正在检查ip可用性...')
        if thread_or_process:
            GetFreeProxy.exec_multi_threading(pool_size, params)
        else:
            GetFreeProxy.exec_multi_process(pool_size, params)
        Log.v('更新完成')
Ejemplo n.º 7
0
    def get_proxys_by_website():
        import time
        headers = {
            'User-Agent':
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
            'Accept':
            'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Upgrade-Insecure-Requests': '1',
            'Host': 'www.xicidaili.com',
            'Pragma': 'no-cache',
            'Connection': 'keep-alive',
            'Cache-Control': 'no-cache',
            'Accept-Language': 'zh-CN,zh;q=0.9'
        }
        response = requests.get('https://www.xicidaili.com/nn/1',
                                headers=headers,
                                timeout=20)
        html = BeautifulSoup(response.text, 'html.parser')
        table = html.find('table', attrs={'id': 'ip_list'})
        trs = table.find_all('tr')
        trs_list = []
        trs_list.extend(trs[1:])
        time.sleep(1)
        response = requests.get('https://www.xicidaili.com/nt/',
                                headers=headers,
                                timeout=20)
        html = BeautifulSoup(response.text, 'html.parser')

        table = html.find('table', attrs={'id': 'ip_list'})
        trs = table.find_all('tr')
        trs_list.extend(trs[1:])
        time.sleep(1)
        response = requests.get('https://www.xicidaili.com/wn/',
                                headers=headers,
                                timeout=20)
        html = BeautifulSoup(response.text, 'html.parser')

        table = html.find('table', attrs={'id': 'ip_list'})
        trs = table.find_all('tr')
        trs_list.extend(trs[1:])
        time.sleep(1)
        response = requests.get('https://www.xicidaili.com/wt/',
                                headers=headers,
                                timeout=20)
        html = BeautifulSoup(response.text, 'html.parser')

        table = html.find('table', attrs={'id': 'ip_list'})
        trs = table.find_all('tr')
        trs_list.extend(trs[1:])

        proxys = []
        for i in range(0, len(trs_list)):
            tds = trs_list[i].find_all('td')
            ip = tds[1].get_text()
            port = tds[2].get_text()
            type = str(tds[5].get_text()).strip(' ')
            date_time = str(tds[9].get_text()).split(' ')[0]
            now = time.strftime("%y-%m-%d", time.localtime())
            # if date_time == now:
            proxys.append(ip + ":" + port + '&' + type)

        proxys = list(set(proxys))
        sqlite = Sqlite(address + 'ip.db')
        results = sqlite.query_data('select count(proxy_adress) from ip_house')
        sqlite = Sqlite(address + 'ip.db')
        num = int(results[0][0])
        for i in range(len(proxys)):
            paras = proxys[i].split('&')
            if paras[0] and GetFreeProxy.verifyProxyFormat(paras[0]):
                sqlite.cursor.execute("INSERT INTO ip_house VALUES (?,?,?,?);",
                                      [num + i, paras[0], paras[1], 'true'])
        sqlite.conn.commit()
        sqlite.close_conn()