Python多线程和代理请求示例

这是一个python多线程调用和代理提交的示例
可以用于负载均衡测试和高并发测试

import hashlib
import json
import random
import sys
import threading
import time
import requests as requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# 请求部分,按顺序使用所有代理
url = ""
url2 = ""
cnt = 0
max_threads = 5
cnt_lock = threading.Lock()

headers = {
    "accept": "application/json, text/javascript, */*; q=0.01",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-TW;q=0.5",
    "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
    "sec-ch-ua": "\"Microsoft Edge\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-requested-with": "XMLHttpRequest",
}

def send_requests(proxy):
    global cnt  # 使用全局的 cnt 变量
    session = requests.Session()
    session.proxies = proxy
    while True:
       

        data1 = {
            
        }
        data2 = {
           
        }
        #F323110310550316454O
        try:
            res = session.post(url=url, headers=headers, data=json.dumps(data1),verify=False,allow_redirects=False)
            res2 = session.post(url=url2, headers=headers, data=json.dumps(data2),verify=False,allow_redirects=False)

            # 如果当前代理地址被屏蔽,可以根据具体条件检查状态码或内容
            # if res.status_code != 200 or res2.status_code != 200 or "alert" in res.text or "alert" in res2.text:
            #     print(f"Proxy {proxy['http']} is blocked. Exiting thread.")
            #     return  # 退出线程
        except requests.exceptions.RequestException as e:
            print(f"Request error with proxy {proxy['http']}: {e}")
            # return  # 出现异常,退出线程

            # 处理响应
        print("\rProcessing: cnt={}, status1={}, status2={}, 当前线程数: {}".format(cnt, res.text, res2.text.strip(),
                                                                                  threading.active_count()), end='', flush=True)
        cnt += 1

# 获取代理信息的部分(与上面的示例代码相同)




_version = sys.version_info
is_python3 = (_version[0] == 3)
orderno = "###"
secret = "###"
ip = "dtan.xiongmaodaili.com"
port = "8088"
ip_port = ip + ":" + port
timestamp = str(int(time.time()))

txt = "orderno=" + orderno + "," + "secret=" + secret + "," + "timestamp=" + timestamp

if is_python3:
    txt = txt.encode()

md5_string = hashlib.md5(txt).hexdigest()
sign = md5_string.upper()

auth = "sign=" + sign + "&" + "orderno=" + orderno + "&" + "timestamp=" + timestamp + "&change=true"
proxy = {"http":"http://" + ip_port, "https": "http://" + ip_port}
headers = {"Xiongmao-Proxy-Authorization": auth, "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}

# headers = {"Proxy-Authorization": auth,"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}
# api_url = ""
#
#
# response = requests.get(api_url)
# print(response)
#     # 定义代理列表
# proxies = []
#
# # 检查响应是否成功
# if response.status_code == 200:
#     data = response.json()
#     if data.get("code") == "0":
#         proxy_list = data.get("obj")
#         if proxy_list:
#             for proxy in proxy_list:
#                 ip = proxy.get("ip")
#                 port = proxy.get("port")
#                 proxy_dict = {
#                     "http": f"http://{ip}:{port}",
#                     "https": f"http://{ip}:{port}"
#                 }
#                 proxies.append(proxy_dict)
#                 print(f"Added Proxy: {ip}:{port}")
#         else:
#             print("Error: No available proxy found.")
#     else:
#         print(f"Error: {data.get('msg')}")
# else:
#     print(f"Error: Failed to retrieve proxy information. Status code: {response.status_code}")

# 创建线程列表
threads = []

# 遍历代理列表并创建线程
for i in range(10):
    thread = threading.Thread(target=send_requests, args=(proxy,))
    threads.append(thread)

# 启动线程
for thread in threads:
    thread.start()

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


使用效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谁的BUG最难改

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值