循环调用接口,出现 Connection aborted.‘, ConnectionAbortedError(10053,你的主机中的软件中止了一个已建立的连接。

# -*- coding: utf-8 -*-
# @Time : 2022/1/11 13:36
import os

import requests
from pathlib import Path
import time


def mkdir(path):
    folder = os.path.exists(path)

    if not folder:  # 判断是否存在文件夹如果不存在则创建为文件夹
        os.makedirs(path)  # makedirs 创建文件时如果路径不存在会创建这个路径


def get_excel(dir_paths):
    """
    pdf文件所在目录
    :param dir_paths:
    :return:
    """
    url = "http://192.168.1.31:56782/table_extract/"
    # 获得所有文件
    dir_path = Path(dir_paths)
    file_paths = list(dir_path.glob('*.*'))
    file_paths = [file_path for file_path in file_paths if file_path.suffix.lower() in ['.pdf']]
    # 循环调用接口
    for file_path_index, file_path in enumerate(file_paths):
        print("第{}份文件开始==========================================".format(file_path_index + 1))
        print('     [{0} / {1}] 服务开始 {2}...'.format(file_path_index + 1, len(file_paths), file_path))
        start_time = time.time()

        # file_path = '/Users/jiongjiongai/data/alpha_insight/ocr/kp/ganggu/港股繁体报告/2021123000355_c.pdf'
        file_path = Path(file_path)

        xlsx_file_path = file_path.with_suffix('.xlsx')
        xlsx_name = xlsx_file_path.name
        # xlsx_file_dir = r"{}\excel_result".format(dir_path)
        xlsx_file_dir = dir_path.joinpath('excel_result')
        # xlsx_file = xlsx_file_dir.joinpath(xlsx_name)

        # 新建文件夹
        mkdir(xlsx_file_dir)

        payload = {}
        files = [
          ('file', (file_path.name, open(str(file_path), 'rb'), 'application/pdf'))
        ]
        headers = {'Connection': 'close'}
        # with requests.Session() as session:
        session = requests.session()
        response = session.post(url, headers=headers, data=payload, files=files, timeout=600)
        requests.session().close()

        # 新建文件
        # xlsx_file = r"{}\excel_result\{}".format(dir_path, xlsx_name)
        xlsx_file = xlsx_file_dir.joinpath(xlsx_name)
        with open(str(xlsx_file), 'wb') as f:
            f.write(response.content)

        duration_sec = time.time() - start_time

        print('     [{0} / {1}] 服务结束 {2} with duration: {3} minutes.'.format(file_path_index + 1, len(file_paths), file_path, int(duration_sec / 60)))
        print("     第{}份文件结束!!!用时{}min".format(file_path_index + 1, int(duration_sec / 60)))


if __name__ == '__main__':
    star_time = time.time()
    get_excel(r'F:\财富趋势')
    end_time = time.time()
    print("总计用时:{}min".format(round(int(end_time - star_time) / 60, 2)))

url没写对,少个/,记录一下,搞死了。。。。操

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个报错是requests库一个异常,表示连接中止了。具体的错误信息是ConnectionAbortedError,错误代码是10053,错误消息是"你的主机软件中止一个建立连接。" 。这个错误通常是由于网络连接问题导致的,比如服务器端关闭了连接或网络断等。这个错误可以通过检查网络连接是否正常、服务器是否正常运行以及使用代理IP等方法来解决 。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Flask请求urllib3.exceptions.ProtocolError: (“Connection broken: ConnectionAborted或BrokenPipeError...](https://blog.csdn.net/farewellnec/article/details/125430480)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [已解决requests.exceptions.ConnectionError: (‘Connection aborted .’, ConnectionResetError(10054,...](https://blog.csdn.net/yuan2019035055/article/details/128307722)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值