Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Properly close ftplib.FTP(). (#7256)

Andrey Rakhmatullin09bd8f42318b
diff --git a/scrapy/pipelines/files.py b/scrapy/pipelines/files.py
index ad4abbbb6..795a0143f 100644
--- a/scrapy/pipelines/files.py
+++ b/scrapy/pipelines/files.py
@@ -391,15 +391,15 @@ class FTPFilesStore:
) -> Deferred[StatInfo]:
def _stat_file(path: str) -> StatInfo:
try:
- ftp = FTP()
- ftp.connect(self.host, self.port)
- ftp.login(self.username, self.password)
- if self.USE_ACTIVE_MODE:
- ftp.set_pasv(False)
- file_path = f"{self.basedir}/{path}"
- last_modified = float(ftp.voidcmd(f"MDTM {file_path}")[4:].strip())
- m = hashlib.md5() # noqa: S324
- ftp.retrbinary(f"RETR {file_path}", m.update)
+ with FTP() as ftp:
+ ftp.connect(self.host, self.port)
+ ftp.login(self.username, self.password)
+ if self.USE_ACTIVE_MODE:
+ ftp.set_pasv(False)
+ file_path = f"{self.basedir}/{path}"
+ last_modified = float(ftp.voidcmd(f"MDTM {file_path}")[4:].strip())
+ m = hashlib.md5() # noqa: S324
+ ftp.retrbinary(f"RETR {file_path}", m.update)
return {"last_modified": last_modified, "checksum": m.hexdigest()}
# The file doesn't exist
except Exception:
diff --git a/tests/test_pipeline_files.py b/tests/test_pipeline_files.py
index 47d516796..6b63dc056 100644
--- a/tests/test_pipeline_files.py
+++ b/tests/test_pipeline_files.py
@@ -61,20 +61,20 @@ def get_ftp_content_and_delete(
password: str,
use_active_mode: bool = False,
) -> bytes:
- ftp = FTP()
- ftp.connect(host, port)
- ftp.login(username, password)
- if use_active_mode:
- ftp.set_pasv(False)
- ftp_data: list[bytes] = []
-
- def buffer_data(data: bytes) -> None:
- ftp_data.append(data)
-
- ftp.retrbinary(f"RETR {path}", buffer_data)
- dirname, filename = split(path)
- ftp.cwd(dirname)
- ftp.delete(filename)
+ with FTP() as ftp:
+ ftp.connect(host, port)
+ ftp.login(username, password)
+ if use_active_mode:
+ ftp.set_pasv(False)
+ ftp_data: list[bytes] = []
+
+ def buffer_data(data: bytes) -> None:
+ ftp_data.append(data)
+
+ ftp.retrbinary(f"RETR {path}", buffer_data)
+ dirname, filename = split(path)
+ ftp.cwd(dirname)
+ ftp.delete(filename)
return b"".join(ftp_data)