обработка ошибки 502 с помощью почтового запроса Python - PullRequest
0 голосов
/ 10 ноября 2018

пытается создать простой скрипт для загрузки данных с несколькими почтовыми запросами. иногда я получаю сообщение об ошибке из-за ответа [504].

Я попытался попробовать / исключить, чтобы обработать эту ошибку, но почему-то я не ловил событие

Я прикрепил код без обработки исключений и изображения ошибки. Я знаю, что ошибка от JSON, потому что нет данных для декодирования из-за ошибки неверного ответа

есть идеи?

import pandas as pd
import requests
import time
import os
from pandas.io.json import json_normalize

df3 = pd.DataFrame()
b = []
current_max = 0
print("downloading first 100 rows data for contract betdiceadmin")
data = {"pos": str(current_max), "offset": "100", "account_name" : "betdiceadmin"}    
request = requests.post(" https://eos.greymass.com/v1/history/get_actions", json=data)   
print(request)
jsonObj = request.json()
df = pd.DataFrame(json_normalize(jsonObj['actions']))
print("finished downloding rows " + str(current_max) +  " to " + str(max(df.account_action_seq)))
b.append(df)
current_max +=100


while max(df.account_action_seq) >= current_max:    
    print("current maximum is "+str(max(df.account_action_seq)))
    time.sleep(5)
    data = {"pos": str(current_max+1), "offset": "99", "account_name" : "betdiceadmin"}
    request = requests.post(" https://eos.greymass.com/v1/history/get_actions", json=data)
    print(request)
    jsonObj = request.json()
    df = pd.DataFrame(json_normalize(jsonObj['actions']))
    current_max +=100
    b.append(df)
    print("max from df is :" + str(max(df.account_action_seq)))


df3 = pd.concat(b, sort=True)

enter image description here

1 Ответ

0 голосов
/ 11 ноября 2018

Вы получили JSONDecodeError, потому что ваш ответ 50x не является JSON. Поэтому вы должны запустить request.json(), когда получите 200, если нет, попробуйте еще раз и подождите немного дольше. Кстати, не добавлять место в URL.

while max(df.account_action_seq) >= current_max:    
    print("current maximum is "+str(max(df.account_action_seq)))
    time.sleep(5)
    data = {"pos": str(current_max+1), "offset": "99", "account_name" : "betdiceadmin"}
    request = requests.post("https://eos.greymass.com/v1/history/get_actions", json=data , verify=False)
    if request.status_code == 200:
        jsonObj = request.json()
        df = pd.DataFrame(json_normalize(jsonObj['actions']))
        current_max +=100
        b.append(df)
        print("max from df is :" + str(max(df.account_action_seq)))
    else:
        print("try to post again")
        continue
...