Python 连接中止请求错误

Python 连接中止请求错误,python,autologin,Python,Autologin,因此,该错误发生在1.5个循环,它进行一个完整循环,并在第二个函数处停止: 目标站点冻结了一段时间,我看到: 有什么问题吗 代码: 导入请求 def vissok(id): 将requests.Session()作为c: url='1〕http://www.worldofdraft.com/ajax/user.php' gameurl=http://www.worldofdraft.com/ajax/entry.php' GPL=['5332','5594','5444','5488','5

因此,该错误发生在1.5个循环,它进行一个完整循环,并在第二个函数处停止:

目标站点冻结了一段时间,我看到:

有什么问题吗

代码:

导入请求
def vissok(id):
将requests.Session()作为c:
url='1〕http://www.worldofdraft.com/ajax/user.php'
gameurl=http://www.worldofdraft.com/ajax/entry.php'
GPL=['5332','5594','5444','5488','5547','5371','5383','5061','5456','5451','5550']
BPL=['5388','5596','5481','5587','5585','5514','5099','5249','5566','5501','5357']
GID=id
UDID='0'
ACT=‘草案’
ACT2='保存草稿'
符号='18852c5f48a94bf3ee58057ff5c016af'
用户名1=email@randommail.com'
用户名2=email2@randommail.com'
密码='supersecretpass'
LOGIN='LOGIN'
c、 获取(url)
login\u data=dict(操作=login,登录=USERNAME1,密码=password)
c、 post(url,数据=登录\数据)
c、 获取(游戏URL)
游戏数据=dict(玩家id=GPL[0],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[1],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[2],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[3],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[4],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[5],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[6],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[7],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[8],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[9],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=GPL[10],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏\数据\保存=dict(动作=ACT2,id=GID,用户\草稿\ id=UDID,符号=sign)
c、 post(游戏URL,数据=游戏数据保存)
def simplebot(id):
将requests.Session()作为c:
url='1〕http://www.worldofdraft.com/ajax/user.php'
gameurl=http://www.worldofdraft.com/ajax/entry.php'
GPL=['5332','5594','5444','5488','5547','5371','5383','5061','5456','5451','5550']
BPL=['5388','5596','5481','5587','5585','5514','5099','5249','5566','5501','5357']
GID=id
UDID='0'
ACT=‘草案’
ACT2='保存草稿'
符号='18852c5f48a94bf3ee58057ff5c016af'
用户名1=email@randommail.com'
用户名2=email2@randommail.com'
密码='supersecretpass'
LOGIN='LOGIN'
c、 获取(url)
login\u data=dict(操作=login,登录=USERNAME2,密码=password)
c、 post(url,数据=登录\数据)
c、 获取(游戏URL)
游戏数据=dict(玩家id=BPL[0],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[1],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[2],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[3],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[4],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[5],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[6],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[7],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[8],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[9],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏数据=dict(玩家id=BPL[10],动作=ACT,id=GID)
c、 post(游戏URL,数据=游戏数据)
游戏\数据\保存=dict(动作=ACT2,id=GID,用户\草稿\ id=UDID,符号=sign)
c、 post(游戏URL,数据=游戏数据保存)
i=184
而我呢,
辛普利波特(一)
维索克(一)
i+=1

所以网站会阻止我发送太多请求。所以必须进行一些节流,所以我在循环中设置了一个延时30秒,在每次“c.post”之后设置了2秒

import time

game_data2 = dict(player_id = BPL[0], action = ACT, id = GID)
c.post(gameurl2, data = game_data2)
time.sleep(2)
循环看起来像:

i = 193
while i < 2000:
    vissok(i)
    simplebot(i)
    print "id: %d done" % i
    time.sleep(30)
    i += 1
i=193
而我<2000:
维索克(一)
辛普利波特(一)
打印“id:%d已完成”%i
时间。睡眠(30)
i+=1

正如我在评论中提到的,您需要限制您的请求。
你已经设法自己解决了这个问题,它应该会起作用

我会提出一个更干净、更畅销的解决方案,您可以创建一个
GET
POST
函数来跟踪您上次提出请求的时间,同时减少代码

看看:

import requests
from time import time, sleep

url = 'http://www.worldofdraft.com/ajax/user.php'
gameurl = 'http://www.worldofdraft.com/ajax/entry.php'
GPL = ['5332', '5594', '5444', '5488', '5547', '5371', '5383', '5061', '5456', '5451', '5550']
BPL = ['5388', '5596', '5481', '5587', '5585', '5514', '5099', '5249', '5566', '5501', '5357']
GID = id
UDID = '0'
ACT = 'draft'
ACT2 = 'save_draft'
SIGN = '18852c5f48a94bf3ee58057ff5c016af'
USERNAME1 = 'email@randommail.com'
USERNAME2 = 'email2@randommail.com'
PASSWORD = 'supersecretpass'
LOGIN = 'login'

last_request = time()
def get(c, url):
    global last_request
    if time()-last_request < 2:
        sleep(2-(time()-last_request))
    ret = c.get(url)
    last_request = time()
    return ret

def post(c, url, data):
    global last_request
    if time()-last_request < 2:
        sleep(2-(time()-last_request))
    ret = c.post(url, data = login_data)
    last_request = time()
    return ret

def vissok(id):
    global url, gameurl, GPL, GID, UDID, ACT, ACT2, SIGN, USERNAME1, PASSWORD, LOGIN
    with requests.Session() as c:
        get(c, url)
        login_data = dict(action = LOGIN, login = USERNAME1, password = PASSWORD)
        post(c, url, data = login_data)

        get(c, gameurl)
        for index in range(0, len(GPL)-1):
            game_data = dict(player_id = GPL[index], action = ACT, id = GID)
            post(c, gameurl, data = game_data)

        game_data_save = dict( action = ACT2, id = GID, user_draft_id = UDID, sign = SIGN)
        post(c, gameurl, data = game_data_save)


def simplebot(id): 
    global url, gameurl, BPL, GID, UDID, ACT, ACT2, SIGN, USERNAME2, PASSWORD, LOGIN
    with requests.Session() as c:

        get(c, url)
        login_data = dict(action = LOGIN, login = USERNAME2, password = PASSWORD)
        post(c, url, data = login_data)

        get(c, gameurl)
        for index in range(0, len(BPL)-1):
            game_data = dict(player_id = BPL[index], action = ACT, id = GID)
            post(c, gameurl, data = game_data)

        game_data_save = dict( action = ACT2, id = GID, user_draft_id = UDID, sign = SIGN)
        post(c, gameurl, data = game_data_save)

i = 184
while i < 189:
    simplebot(i)
    vissok(i)
    i += 1
    sleep(30) # Not sure why you want to wait 30 seconds here, but i kept it.
并将其循环,因为唯一改变的是
0
->
1
->
2
等等

我还创建了一个
get()
post()
函数,该函数基本上完成了刚才所做的工作,但添加了一个
sleep()
,它计算2秒与自上次请求以来的时间差,因此它将在每次get和post请求之间“动态”等待2秒

我还没有验证过这段代码,但希望它能让您对未来的编程有所了解


祝你好运

你需要限制你的请求。基本的机器人写作,你不能像你一样锤网站感谢我介绍油门的概念!通常情况下,您会让其他人在堆栈溢出上回答您的问题,这样人们就可以得到他们应得的分数。这是促使人们首先给出答案的原因之一
import requests
from time import time, sleep

url = 'http://www.worldofdraft.com/ajax/user.php'
gameurl = 'http://www.worldofdraft.com/ajax/entry.php'
GPL = ['5332', '5594', '5444', '5488', '5547', '5371', '5383', '5061', '5456', '5451', '5550']
BPL = ['5388', '5596', '5481', '5587', '5585', '5514', '5099', '5249', '5566', '5501', '5357']
GID = id
UDID = '0'
ACT = 'draft'
ACT2 = 'save_draft'
SIGN = '18852c5f48a94bf3ee58057ff5c016af'
USERNAME1 = 'email@randommail.com'
USERNAME2 = 'email2@randommail.com'
PASSWORD = 'supersecretpass'
LOGIN = 'login'

last_request = time()
def get(c, url):
    global last_request
    if time()-last_request < 2:
        sleep(2-(time()-last_request))
    ret = c.get(url)
    last_request = time()
    return ret

def post(c, url, data):
    global last_request
    if time()-last_request < 2:
        sleep(2-(time()-last_request))
    ret = c.post(url, data = login_data)
    last_request = time()
    return ret

def vissok(id):
    global url, gameurl, GPL, GID, UDID, ACT, ACT2, SIGN, USERNAME1, PASSWORD, LOGIN
    with requests.Session() as c:
        get(c, url)
        login_data = dict(action = LOGIN, login = USERNAME1, password = PASSWORD)
        post(c, url, data = login_data)

        get(c, gameurl)
        for index in range(0, len(GPL)-1):
            game_data = dict(player_id = GPL[index], action = ACT, id = GID)
            post(c, gameurl, data = game_data)

        game_data_save = dict( action = ACT2, id = GID, user_draft_id = UDID, sign = SIGN)
        post(c, gameurl, data = game_data_save)


def simplebot(id): 
    global url, gameurl, BPL, GID, UDID, ACT, ACT2, SIGN, USERNAME2, PASSWORD, LOGIN
    with requests.Session() as c:

        get(c, url)
        login_data = dict(action = LOGIN, login = USERNAME2, password = PASSWORD)
        post(c, url, data = login_data)

        get(c, gameurl)
        for index in range(0, len(BPL)-1):
            game_data = dict(player_id = BPL[index], action = ACT, id = GID)
            post(c, gameurl, data = game_data)

        game_data_save = dict( action = ACT2, id = GID, user_draft_id = UDID, sign = SIGN)
        post(c, gameurl, data = game_data_save)

i = 184
while i < 189:
    simplebot(i)
    vissok(i)
    i += 1
    sleep(30) # Not sure why you want to wait 30 seconds here, but i kept it.
game_data = dict(player_id = BPL[0], action = ACT, id = GID)
c.post(gameurl, data = game_data)
game_data = dict(player_id = BPL[1], action = ACT, id = GID)
c.post(gameurl, data = game_data)