Skip to content

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

Use context management with `FTP`

OmarFarragc544c0d2b835
diff --git a/scrapy/utils/ftp.py b/scrapy/utils/ftp.py
index 752e3c953..9992a916e 100644
--- a/scrapy/utils/ftp.py
+++ b/scrapy/utils/ftp.py
@@ -22,13 +22,12 @@ def ftp_store_file(
"""Opens a FTP connection with passed credentials,sets current directory
to the directory extracted from given path, then uploads the file to server
"""
- ftp = FTP()
- ftp.connect(host, port)
- ftp.login(username, password)
- if use_active_mode:
- ftp.set_pasv(False)
- file.seek(0)
- dirname, filename = posixpath.split(path)
- ftp_makedirs_cwd(ftp, dirname)
- ftp.storbinary('STOR %s' % filename, file)
- ftp.quit()
+ with FTP() as ftp:
+ ftp.connect(host, port)
+ ftp.login(username, password)
+ if use_active_mode:
+ ftp.set_pasv(False)
+ file.seek(0)
+ dirname, filename = posixpath.split(path)
+ ftp_makedirs_cwd(ftp, dirname)
+ ftp.storbinary('STOR %s' % filename, file)