您可以上传文件,但不能上传变量。
您可以使用BytesIO
or StringIO
使用您的数据创建文件并上传。它们具有像普通文件一样的功能 - 即。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
你可以使用模块json
用所有数据创建字符串
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:带模块的示例csv
不能直接使用BytesIO
但它需要字符串文件。
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())