Ошибка при парсинге. ftplib.error_reply: 27 Entering Passive Mode (Возникает в рандомное время)
Был написан парсер для сбора данных с сервера. После запуска через рандомное время он выдавал ошибку 425 Timeout. Нашел библиотеку с реконнектом, которая решила данную проблему. Теперь начала возникать новая (может возникнуть при старте, а может и спустя 2 часа) ftplib.error_reply: 27 Entering Passive Mode. Подскажите, пожалуйста, решение данной проблемы.
import os
import zipfile
import xml.etree.ElementTree as ET
from ftplib import FTP_TLS
from csv import writer
import reconnecting_ftp
data = []
def get_info_from_xml(path_to_file: str) -> list:
ns1 = 'http://zakupki.gov.ru/oos/export/1'
ns2 = 'http://zakupki.gov.ru/oos/CPtypes/1'
foundation_info_tag = '{' + ns2 + '}foundationInfo'
purchase_number_tag = '{' + ns2 + '}purchaseNumber'
participant_info_tag = '{' + ns2 + '}participantInfo'
inn_tag1 = '{' + ns2 + '}taxPayerCode'
inn_tag = '{' + ns2 + '}INN'
tree = ET.parse(path_to_file)
root = tree.getroot()
global purchase_number
global inn
for child_root in root:
for elem in child_root:
if elem.tag == foundation_info_tag:
purchase_number = elem.find(purchase_number_tag).text
if elem.tag == participant_info_tag:
try:
inn = elem[0].find(inn_tag).text
except AttributeError:
inn = elem[0].find(inn_tag1).text
return [purchase_number, inn]
with reconnecting_ftp.Client(hostname="ftp.zakupki.gov.ru", port=21, user="free", password="free") as ftp:
ftp.cwd('fcs_regions/Astrakhanskaja_obl/contractprojects/prevMonth')
for current_file in ftp.nlst():
print('*****************************************')
print(f'Обрабатываю файл {current_file}')
with open(f'contract_projects/{current_file}', 'wb') as fp:
ftp.retrbinary(f'RETR {current_file}', fp.write, blocksize=1024 * 1024)
current_zip = zipfile.ZipFile(file=f'contract_projects/{current_file}', mode='r')
for current_file_in_zip in current_zip.namelist():
if current_file_in_zip.endswith('xml'):
print('---------------------------------------------------')
print(f'Обрабатываю файл {current_file_in_zip} внутри архива')
current_zip.extract(current_file_in_zip, path='current_file')
purchase_number, inn = get_info_from_xml(f'current_file/{current_file_in_zip}')
a = [inn, purchase_number]
data.append(a)
print(f'Файл {current_file_in_zip} внутри архива обработан')
os.remove(f'current_file/{current_file_in_zip}')
print(f'Файл {current_file} обработан\n---------------------------------------')
current_zip.close()
os.remove(f'contract_projects/{current_file}')
with open('nomera_i_inn.csv', mode='a', newline='') as csv_file:
csv_writer = writer(csv_file)
for item in data:
csv_writer.writerow([item])
print(item)