如何在 Python 3 中通过 FTP 从内存上传数据?

How to upload data from memory via FTP in Python 3?(如何在 Python 3 中通过 FTP 从内存上传数据?)
本文介绍了如何在 Python 3 中通过 FTP 从内存上传数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想通过 FTP 将内存中的各种数据(数组内容、静态 html 代码……)上传到网络服务器.

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

这仅适用于一个基本字符串Hello World":

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>

推荐答案

可以上传文件,但不能上传变量.

You can upload files but not variables.

您可以使用 BytesIOStringIO 使用您的数据创建文件并上传.它们具有像普通文件一样的功能 - 即.bio.write(html_code.encode()).

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)

对于 datalog,您可以使用模块 json 来创建包含所有数据的字符串

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)


带有模块 csv 的示例不能直接与 BytesIO 一起使用,但它需要字符串文件.


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()) 

这篇关于如何在 Python 3 中通过 FTP 从内存上传数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)