954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python FTP script to upload file

Hi All,
Currently Iam writing python ftp script to upload a file to FTP Server.
The server where iam going to download a file expects the upload command in this format
"put flash"

I dont know how to send this same command in ftp session after login.

I have tried sendcmd call in this way, but its not working

comand='put send.bin flash'
ftp.sendcmd('STOR'+comand)


I need a suggestion from this group.


thanks

nirmalarasu
Newbie Poster
12 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

put <filename> flash
is an FTP client command that says "Take my local and copy it to the server as a file named flash".

So the correct call would be ftp.sendcmd('STOR flash')

It would then be up to you to follow that up with data from the local file

BearofNH
Posting Whiz
323 posts since May 2007
Reputation Points: 94
Solved Threads: 48
 

Hi,
Still iam in loop.
Can you please explain , how to send the local file name after passing the command
"ftp.sendcmd('STOR flash')"

thanks for your response

nirmalarasu
Newbie Poster
12 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Yes I got the solution after few research.
Iam posting my solution for others .

import ftplib
import os

def upload(ftp, file):
ext = os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR flash " + file, open(file))
else:
print ftp.storbinary("STOR flash" + file, open(file, "rb"), 1024)


ftp = ftplib.FTP("**.**.**.**")
print ftp.login("ftp", "flash")
upload(ftp, "filename")

nirmalarasu
Newbie Poster
12 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You