本帖最后由 sun4ay 于 2023-7-24 08:47 编辑
改成每秒钟发包10次 requests改成 grequests 还有就是时间能不能优化一下
[Python] 纯文本查看 复制代码 import time
from datetime import datetime
from datetime import timedelta
from datetime import timezone
import requests
from fake_useragent import UserAgent
import grequests
url = 'https://www.baidu.com'
ua = UserAgent()
headers = {
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.43',
'Tyauthtoken': '4FE86EA2C64241BCA0220AFB0C517C2F', # 改账号密码
"User-Agent": ua.random
}
json = {
1231231
123123
}
def execute_command(num):
#data = requests.post(url=url, headers=headers, json=json)
data = grequests.requests("post",url=url, headers=headers, json=json)
print(data.json())
status = data.json()['status']
utc_now = datetime.utcnow().replace(tzinfo=timezone.utc)
SHA_TZ = timezone(
timedelta(hours=8),
name='Asia/Shanghai',
)
beijing_now = utc_now.astimezone(SHA_TZ)
print(beijing_now)
now_fmt = beijing_now.strftime('%H:%M:%S:%f')
print(now_fmt)
if status == 301:
print("第{}请求失败".format(num))
num = num + 1
time.sleep(0.1)
execute_command(num)
elif status == 500:
print("第{}请求失败".format(num))
num = num + 1
time.sleep(1)
execute_command(num)
else:
print('成功')
def wait_until_8pm():
target_time = datetime.now().replace(hour=7, minute=2, second=59, microsecond=504500)
while True:
now = datetime.now()
if now >= target_time:
break
time.sleep(0.0001)
if __name__ == "__main__":
wait_until_8pm()
execute_command(1) |