Search code examples
pythonftpuploadftplib

How to upload data from memory via FTP in Python 3?


I want to upload various data from memory (contents of arrays, static html-code, ...) to a webserver via FTP.

This works for just one basic string 'Hello World':

from ftplib import FTP
import io
...

bio = io.BytesIO(b'Hello World')
ftp.storbinary('STOR index.html', bio)

However, I do not get it right to upload data like:

datalog = array([['Temperature', 0, 0], ['Humidity', 0, 0]])
html_code = '<head><title></title></head><body>display here</body></html>

Solution

  • You can upload files but not variables.

    You can use BytesIO or StringIO to create file with your data and upload it. They have functions like normal file - ie. bio.write(html_code.encode()).

    from ftplib import FTP
    import io
    
    text = '<head><title></title></head><body>display here</body></html>'
    
    bio = io.BytesIO()
    bio.write(text.encode())
    bio.seek(0)  # move to beginning of file
    
    ftp.storbinary('STOR index.html', bio)
    

    For datalog you can use module json to create string with all data

    from ftplib import FTP
    import io
    
    import json
    
    datalog = ([['Temperature', 0, 0], ['Humidity', 0, 0]])
    text = json.dumps(datalog)
    
    bio = io.BytesIO()
    bio.write(text.encode())
    bio.seek(0)  # move to beginning of file
    
    ftp.storbinary('STOR data.json', bio)
    

    EDIT: Example with module csv which can't work directly with BytesIO but it needs string file.

    from ftplib import FTP
    import io
    
    import csv
    
    data = [['Temperature', 0, 0], ['Humidity', 0, 0]]
    
    bio = io.BytesIO()
    iow = io.TextIOWrapper(bio)  # create String wrapper
    
    csv_writer = csv.writer(iow) # create csv writer
    csv_writer.writerows(data)   # write all rows
    
    iow.flush()  # force String to send all from buffer to file (you can't use `iow.close()` for it)
    bio.seek(0)  # move to beginning of file
    
    ftp.storbinary('STOR data.csv', bio)
    
    # to see what is in bio
    #bio.seek(0)
    #print(bio.read())