Ejemplo n.º 1
0
async def run(num):
    tasks = []
    # Semaphore, 相当于基于服务器的处理速度和测试客户端的硬件条件,一批批的发
    # 直至发送完全部(下面定义的number/num)
    sem = asyncio.Semaphore(2000)
    # 创建session,且对本地的TCP连接不做限制limit=0
    # 超时时间指定
    # total:全部请求最终完成时间
    # connect: aiohttp从本机连接池里取出一个将要进行的请求的时间
    # sock_connect:单个请求连接到服务器的时间
    # sock_read:单个请求从服务器返回的时间
    import aiohttp
    # timeout = aiohttp.ClientTimeout(total=300, connect=60, sock_connect=60, sock_read=60)
    async with ClientSession(connector=TCPConnector(limit=0)) as session:
        while True:
            cookie_jar = PushTool.get_cookies()
            for i in range(0, num):
                # 如果是分批的发,就使用并传递Semaphore
                task = asyncio.ensure_future(
                    bound_register(sem=sem,
                                   session=session,
                                   cookie_jia=cookie_jar))
                tasks.append(task)
            responses = asyncio.gather(*tasks)
            await responses
Ejemplo n.º 2
0
import requests
from tools.push_tools import PushTool
from urllib import parse

success_code = 0
fail_code = 0
server = PushTool.https_target()
cookie = PushTool.get_cookies()


def https_push(domain):
    global success_code
    global fail_code
    url = ''
    while True:
        try:
            r = PushTool.rand_all(domain)
            i = PushTool.rand_all(domain)
            headers = {
                'User-Agent': PushTool.user_agent(),
                'Referer': r,
                'Accept': '*/*',
                'Connection': 'keep-alive',
                'Host': 'sp0.baidu.com',
            }
            conn = requests.Session()
            conn.headers = headers
            # print(headers)
            # 将cookiesJar赋值给会话
            cookiesJar = requests.utils.cookiejar_from_dict(cookie, cookiejar=None, overwrite=True)
            conn.cookies = cookiesJar