代理ip 多线程 python写法/ ip池

先封装

import requests
import copy


def ipGet():


    while True:
        try:
            url = "http://api1.xxxx&format=txt"
            response = requests.get(url)
            ip = response.text.strip()
            proxy = {
                'http': 'http://' + ip,
                'https': 'http://' + ip
            }
            proxys = copy.deepcopy(proxy)
            print(proxy)

            page = requests.get('https://www.baidu.com', proxies=proxy, timeout=(2, 2))
            if page.status_code == 200:
                print(ip)
                return proxys
            else:
                print('无效的ip')
        except Exception as e:
            print(e)



if __name__ == '__main__':
    ipGet()

例子

import schedule
import threading
import time
from loguru import logger
import requests
import sys
from fake_useragent import UserAgent
from get_proxy import ipGet


ua = UserAgent()
# print(ua.random)

logger.add("debug1.log", encoding="utf-8")

cookies = {
    'xxxx': 'xxxxx',
}

headers = {
    'User-Agent': ua.random,
    'Content-Type': 'application/json',
    'Origin': 'https://xxxxx.com',
    'ax': 'xxxxx',
    'xweb_xhr': '1',
    'X-APP-APPID': '3',
    'App-Client-Id': '6',
    'Sec-Fetch-Site': 'cross-site',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'https://xxxxx.html',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    # 'Cookie': 'xxxxx',
}

json_data = '{xxxxx}'

# 创建一个全局事件对象,用于在线程间通信
stop_event = threading.Event()







def post_ticket():
    try:
        proxy = ipGet()
        logger.info(f'使用得代理:{proxy}')
        response = requests.post(
            'https://xxxxxx',
            proxies=proxy,
            cookies=cookies,
            headers=headers,
            data=json_data,
        )

        logger.info(response.text)

        # 检查响应内容,如果成功则设置事件,停止所有线程
        # if any(keyword in str(response.text) for keyword in ['成功', 'xxx']):
        if any(keyword in str(response.text) for keyword in ['成功']):
            logger.info("成功,停止所有线程。")
            stop_event.set()  # 设置事件,通知其他线程停止
            return True  # 返回成功状态
    except Exception as e:
        logger.error(e)
    return False  # 返回失败状态

def task():
    threads = []
    for i in range(10):  # 创建 10 个线程
        t = threading.Thread(target=post_ticket_multiple)
        threads.append(t)
        t.start()

    # 等待所有线程完成
    for t in threads:
        t.join()

def post_ticket_multiple():
    for _ in range(20):  # 每个线程发送 50 个请求
        if stop_event.is_set():  # 检查是否需要停止线程
            logger.info("检测到停止信号,线程终止。")
            break
        if post_ticket():  # 如果成功,停止当前线程
            break
        time.sleep(0.1)

logger.info('test one...')
post_ticket()
logger.info('test wait...')

# 安排任务在 12:59:59 执行
schedule.every().day.at("16:59:59").do(task)

while True:
    schedule.run_pending()
    time.sleep(1)  # 避免 CPU 占用过高


ip池demo

import schedule
import threading
import time
from loguru import logger
import requests
import random
import sys
from fake_useragent import UserAgent
from get_proxy import ipGet


# 初始化 UserAgent 和日志
ua = UserAgent()
logger.add("debugdailichi.log", encoding="utf-8")

cookies = {
    'xxxx': 'xxxx',
}

headers = {
    'User-Agent': ua.random,
    'Content-Type': 'application/json',
    'Origin': 'https:xxxxx5.com',
    'ax': 'xxx',
    'xweb_xhr': '1',
    'X-APP-APPID': '3',
    'App-Client-Id': '6',
    'Sec-Fetch-Site': 'cross-site',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'https://sxxxx.html',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    # 'Cookie': 'xxxx',
}

json_data = '{xxxx}'



# 全局变量
stop_event = threading.Event()
NUM_THREADS = 10
REQUESTS_PER_THREAD = 20

# 初始化代理池
proxy_pool = []



# 加载初始代理池
def init_proxy_pool(size=10):
    global proxy_pool
    proxy_pool = [ipGet() for _ in range(size)]
    logger.info(f"初始化代理池:{proxy_pool}")


# 获取可用代理
def get_valid_proxy():
    return random.choice(proxy_pool)


# 定期更新代理池
def update_proxy_pool():
    while not stop_event.is_set():
        time.sleep(60)  # 每隔60秒更新一次代理池
        new_proxies = [ipGet() for _ in range(5)]  # 获取新代理
        proxy_pool.extend(new_proxies)
        logger.info(f"代理池更新:{new_proxies}")


# 抢票函数
def post_ticket():
    proxy = get_valid_proxy()
    try:
        response = requests.post(
            'https://xxxxx/generateOrder',
            proxies=proxy,
            cookies=cookies,
            headers=headers,
            data=json_data,
            timeout=5
        )
        logger.info(response.text)

        if '成功' in response.text:
            logger.info("xxxx成功,停止所有线程。")
            stop_event.set()
            return True
        if 'xx不足' in response.text:
            logger.info("xxxx不足,停止所有线程。")
            stop_event.set()
            return True
    except requests.RequestException as e:
        logger.error(f"请求异常: {e}")
        if proxy in proxy_pool:
            proxy_pool.remove(proxy)  # 移除失效代理
            logger.info(f"移除失效代理:{proxy}")
    return False


# 多线程管理函数
def task():
    threads = [threading.Thread(target=post_ticket_multiple, name=f"Thread-{i + 1}") for i in range(NUM_THREADS)]
    for t in threads:
        t.start()
    for t in threads:
        t.join()


# 每个线程的工作逻辑
def post_ticket_multiple():
    for _ in range(REQUESTS_PER_THREAD):
        if stop_event.is_set():
            logger.info(f"{threading.current_thread().name} 检测到停止信号,线程终止。")
            break
        if post_ticket():
            break
        time.sleep(0.1)


# 启动定时任务和代理池维护线程
if __name__ == "__main__":
    logger.info("程序启动...")
    init_proxy_pool()  # 初始化代理池
    threading.Thread(target=update_proxy_pool, daemon=True).start()  # 启动代理池更新线程

    schedule.every().day.at("11:41:00").do(task)

    while not stop_event.is_set():
        schedule.run_pending()
        # time.sleep(1)

代理

推荐一:http://www.ydaili.cn//main/register.aspx?str_code=LNP82T86

posted @ 2024-11-23 15:12  __username  阅读(3)  评论(0编辑  收藏  举报

本文作者:DIVMonster

本文链接:https://www.cnblogs.com/guangzan/p/12886111.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。