连接中止请求错误

时间:2015-12-29 06:06:06

标签: python autologin

所以这个错误发生在1.5循环,它产生一个完整的循环并停在第二个函数:

enter image description here

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

enter image description here

问题是什么?

代码:

import requests

def vissok(id):
    with requests.Session() as c:
    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'
    c.get(url)
    login_data = dict(action = LOGIN, login = USERNAME1, password = PASSWORD)
    c.post(url, data = login_data)

    c.get(gameurl)
    game_data = dict(player_id = GPL[0], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[1], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[2], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[3], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[4], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[5], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[6], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[7], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[8], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[9], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = GPL[10], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data_save = dict( action = ACT2, id = GID, user_draft_id = UDID, sign = SIGN)
    c.post(gameurl, data = game_data_save)


def simplebot(id):  
with requests.Session() as c:
    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'
    c.get(url)
    login_data = dict(action = LOGIN, login = USERNAME2, password = PASSWORD)
    c.post(url, data = login_data)

    c.get(gameurl)
    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)
    game_data = dict(player_id = BPL[2], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = BPL[3], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = BPL[4], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = BPL[5], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = BPL[6], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = BPL[7], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = BPL[8], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = BPL[9], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data = dict(player_id = BPL[10], action = ACT, id = GID)
    c.post(gameurl, data = game_data)
    game_data_save = dict( action = ACT2, id = GID, user_draft_id = UDID, sign = SIGN)
    c.post(gameurl, data = game_data_save)


i = 184
while i < 189:
    simplebot(i)
    vissok(i)
    i += 1

2 个答案:

答案 0 :(得分:0)

所以网站阻止我发送太多请求。因此必须进行一些限制,所以我在循环中延迟了30秒和2秒后的时间延迟&#39; c.post&#39;

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.autobus);

循环看起来像:

private String saveToInternalSorage(Bitmap bitmapImage, String imagename) throws IOException{
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
     // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,imagename);

    FileOutputStream fos = null;
    try {           
        fos = new FileOutputStream(mypath);
   // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
          e.printStackTrace();
    } finally {
          fos.close(); 
    } 
    return directory.getAbsolutePath();
}

答案 1 :(得分:0)

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

我建议使用更简洁,更实用的解决方案,您可以创建GETPOST函数,跟踪上次发出请求的时间,并减少代码。

看看:

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.

基本上我所做的就是将所有变量放在代码顶部并将它们设置为全局变量(因为您使用了完全相同的变量两次,这样可以节省代码中的大量空间重用它们而不是复制它们。)

然后我删除了您所做的所有单独的c.post来电:

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)

并制定了一个循环,因为唯一改变的是0 - &gt; 1 - &gt; 2等等。

我还创建了一个get()post()函数,它基本上完成了你刚刚做的事情,但添加了一个sleep()来计算2秒和自上次请求以来的时间之间的差异,所以它会动态地等待#34;每次GET和POST请求之间最多2秒钟。

Haven未验证此代码,但希望它能让您了解将来如何使用编程。

祝你好运!