Question

I'm using the following code to upload a html file to my website however when uploading it seems to be missing some data...

My content is 1930 lines with a "length" of 298872 (I guess that's how many characters)

## Login using the ftplib library and set the session as the variable ftp_session
ftp_session = ftplib.FTP('ftp.website.com','admin@website.com','password')

## Open a file to upload
html_ftp_file = open('OUTPUT/output.html','rb')
## Open a folder under in the ftp server
ftp_session.cwd("/folder")
## Send/upload the the binary code of said file to the ftp server
ftp_session.storbinary('STOR output.html', html_ftp_file )
## Close the ftp_file
html_ftp_file.close()

## Quit out of the FTP session
ftp_session.quit()

Why isn't this uploading 100% of the file? it's uploading like 98% of it...

I've been looking around and can't find out what the max character limit is or the max file size is, could this be fixed by uploading it in parts (I'm unsure how to do that)

Passive

*cmd* 'CWD /folder'
*resp* '250 OK. Current directory is /folder'
*cmd* 'TYPE A'
*resp* '200 TYPE is now ASCII'
*cmd* 'PASV'
*resp* '227 Entering Passive Mode (xxx,xxx,xxx,xxx,73,19)'
*cmd* 'STOR output.html'
*resp* '150 Accepted data connection'
*resp* '226-File successfully transferred'
*resp* '226 3.235 seconds (measured here), 48.23 Kbytes per second'
*cmd* 'QUIT'
*resp* '221-Goodbye. You uploaded 157 and downloaded 0 kbytes.'
*resp* '221 Logout.'

Active

*cmd* 'CWD /folder'
*resp* '250 OK. Current directory is /folder'
*cmd* 'TYPE A'
*resp* '200 TYPE is now ASCII'
*cmd* 'PORT xxx,xxx,xxx,xxx,203,212'
*resp* '200 PORT command successful'
*cmd* 'STOR output.html'
*resp* '150 Connecting to port 52180'
*resp* '226-File successfully transferred'
*resp* '226 4.102 seconds (measured here), 38.03 Kbytes per second'
*cmd* 'QUIT'
*resp* '221-Goodbye. You uploaded 157 and downloaded 0 kbytes.'
*resp* '221 Logout.'
Was it helpful?

Solution

From your code it looks like your FTP mode is binary, but you are uploading a ASCII file (html). Try changing your FTP mode to ASCII or zip your file first (which would be a binary file), send it and then unzip at your destination.

Here is and eaxmple from http://effbot.org/librarybook/ftplib.htm

import ftp
import os

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

ftp = ftplib.FTP("ftp.fbi.gov")
ftp.login("mulder", "trustno1")

upload(ftp, "trixie.zip")
upload(ftp, "file.txt")
upload(ftp, "sightings.jpg")

OTHER TIPS

Try the following which will upload the file in binary mode. The trick is to set the type of file transfer to binary mode (TYPE I) before calling storbinary.

with open('OUTPUT/output.html','rb') as html_ftp_file:
    # ftp_session.set_pasv(1) # If you want to use passive mode for transfer.
    ftp_session.voidcmd('TYPE I')  # Set file transfer type to Image (binary)
    ftp_session.cwd("/folder")
    ftp_session.storbinary('STOR output.html', html_ftp_file)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top