互联网断开后如何继续请求发布

时间:2019-04-04 15:24:15

标签: python python-3.x python-requests

with open("student.csv", "r") as csv_ledger:
    r = csv.DictReader(csv_ledger)
    data = [dict(d) for d in r ]
    groups = {}

    for k, g in groupby(data, lambda r: (r['name'])):
        items = []
        for i in g:
        #data processing
        try:
           post_api = requests.post(ENDPOINT_URL, json=groups, headers=headers)
        except requests.ConnectionError:
              print("Something went wrong")
        finally:
              print("resume post request")

当前,当互联网连接断开时,我的代码将无法恢复发帖请求。如果我使用try和exception,它将无法正常工作。

2 个答案:

答案 0 :(得分:0)

不确定,它是否可以像这样工作。

可恢复请求的想法是先分块发送数据,然后将其组装为后端,因此,如果请求之间发生请求失败,则可以稍后通过发送其他分块来恢复请求。 后端也应该能够接受大块并将其组装

看看python库resumable

答案 1 :(得分:0)

不确定在没有服务器端实现的情况下也可以按需恢复,但是可以从客户端恢复。这是一个简单的阻止示例,但您可能希望将其放入线程中。

import csv
import socket
from time import sleep

import requests


def is_internet_on():
    try:
        socket.setdefaulttimeout(3)
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(('1.1.1.1', '8080'))
        return True
    except:
        return False


def do_call_later(url, headers, body):
    while not is_internet_on():
        sleep(5)
    requests.post(url, headers=headers, body=body)


with open("student.csv", "r") as csv_ledger:
    r = csv.DictReader(csv_ledger)
    data = [dict(d) for d in r]
    groups = {}

    for k, g in groupby(data, lambda r: (r['name'])):
        items = []
        for i in g:
            # data processing
            pass
        try:
            timeout_arg = (
                # first tuple value is the connection timeout,
                # how long to wait before initial connection is established
                1.0,
                # second tuple value is the read timeout, this is how long
                # the client will wait after the initial connection
                # before dropping the connection because no response was sent
                1.0
            )
            post_api = requests.post(ENDPOINT_URL, json=groups, headers=headers, timeout=timeout_arg)
        except requests.ConnectionError:
            do_call_later(ENDPOINT_URL, headers, groups)
        finally:
            print("resume post request")

编辑:超时呼叫文档:https://github.com/kennethreitz/requests/blob/master/requests/api.py#L34