Ad
Ftplib Is Timing Out
I'am attempting to send 5 files over to an ftp server. However, only the first 3 out of the 5 files are sent, while the last 2 are not sent.
I suspect it has to do with timing out but im not sure, as it takes around 5 minutes for the first 3 files to be transferred.
Any suggestions?
##OLD CODE
import subprocess
#ftp credentials
server='myserver.host.com'
user='[email protected]'
password='mypassword'
#logging in to ftp
proc =subprocess.Popen(['ftp -p {}'.format(server)],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#sending username
proc.stdin.write(bytes(user+'\n',encoding='UTF-8'))
proc.stdin.flush()
#sending password
proc.stdin.write(bytes(password+'\n',encoding='UTF-8'))
proc.stdin.flush()
#name of ftp directory were files will be transferred
ftpdirectory='ftpdir'
#list of file directories to be transferred
filestotransfer=['/home/user/Desktop/file1.csv','/home/user/Desktop/file2.csv','/home/user/Desktop/file3.csv','/home/user/Desktop/file4.csv','/home/user/Desktop/file5.csv']
#transferring files one by one
for i in filestotransfer:
filename=i.split('/')[-1]
syntaxx='put "{}" "/{}/{}"'.format(i,ftpdirectory,filename) #put /home/user/Desktop/file1.csv /ftpdir/file1.csv
proc.stdin.write(bytes(syntaxx + '\n', encoding='UTF-8'))
proc.stdin.flush()
##SOLUTION
import ftplib
import os.path
server = "myserver.host.com"
user = "[email protected]"
password = "mypassword"
ftpdirectory = "/ftpdir"
filestotransfer = [
"/home/user/Desktop/file1.csv",
"/home/user/Desktop/file2.csv",
"/home/user/Desktop/file3.csv",
"/home/user/Desktop/file4.csv",
"/home/user/Desktop/file5.csv",
]
for i in filestotransfer:
try:
with ftplib.FTP(server,timeout=10) as ftp:
print('logging in')
ftp.login(user, password)
with open(i, "rb") as fp:
dest_path = os.path.join(ftpdirectory, os.path.basename(i))
ftp.storbinary(f"STOR {dest_path}", fp)
print('completed sending {}'.format(i))
ftp.close()
except:
print('timedout')
Ad
Answer
As I mentioned in the comments, ftplib
is a built-in module in Python.
Your script simplifies to something like
import ftplib
import os.path
import posixpath
server = "myserver.host.com"
user = "[email protected]"
password = "mypassword"
ftpdirectory = "/ftpdir"
filestotransfer = [
"/home/user/Desktop/file1.csv",
"/home/user/Desktop/file2.csv",
"/home/user/Desktop/file3.csv",
"/home/user/Desktop/file4.csv",
"/home/user/Desktop/file5.csv",
]
with ftplib.FTP(server) as ftp:
ftp.login(user, password)
for filepath in filestotransfer:
with open(filepath, "rb") as fp:
dest_path = posixpath.join(ftpdirectory, os.path.basename(filepath))
print(f"Transferring {filepath} to {dest_path}")
ftp.storbinary(f"STOR {dest_path}", fp)
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad