久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 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 從內存上傳數據?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標)
主站蜘蛛池模板: 亚洲国产高清在线观看 | yiren22综合网成人 | 久久99这里只有精品 | 色综合99| 国产一区二| 日韩成人免费视频 | 国产一区二区三区四区三区四 | 日中文字幕在线 | 日韩羞羞 | 伊人春色在线 | 精品中文在线 | 久久精品欧美一区二区三区麻豆 | 日本久久福利 | 久久久欧洲 | 久久一区精品 | 国产免费福利小视频 | 欧美一级二级视频 | 黑人精品欧美一区二区蜜桃 | 亚洲第一av网站 | 国内精品久久影院 | 日韩精品一区二区三区久久 | 亚洲美女天堂网 | 91精品国产综合久久久久久蜜臀 | 中文字幕一区二区三区四区 | 精品国产18久久久久久二百 | 欧美夜夜| 日韩欧美三级在线 | 亚洲精品乱码久久久久久按摩 | 91免费在线 | 天堂一区二区三区四区 | 国产精品波多野结衣 | 天天躁日日躁狠狠的躁天龙影院 | 日韩精品久久久久 | 国产精品福利网 | 久久精品网 | 国产综合久久久 | 91网在线播放 | 五月精品视频 | 久久成人18免费网站 | 欧美成人a| 欧美亚洲视频在线观看 |