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

如何在 Python 3 中通過 FTP 從內存上傳數(shù)據(jù)?

How to upload data from memory via FTP in Python 3?(如何在 Python 3 中通過 FTP 從內存上傳數(shù)據(jù)?)
本文介紹了如何在 Python 3 中通過 FTP 從內存上傳數(shù)據(jù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想通過 FTP 將內存中的各種數(shù)據(jù)(數(shù)組內容、靜態(tài) html 代碼……)上傳到網(wǎng)絡服務器.

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)

但是,我無法正確上傳以下數(shù)據(jù):

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 使用您的數(shù)據(jù)創(chuàng)建文件并上傳.它們具有像普通文件一樣的功能 - 即.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 來創(chuàng)建包含所有數(shù)據(jù)的字符串

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

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

相關文檔推薦

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:“浮動對象不可下標)
主站蜘蛛池模板: 一道本在线 | 在线一区视频 | 欧美性吧 | 欧美极品一区二区 | 中文字幕在线观看第一页 | 毛片网在线观看 | 中文字幕成人在线 | 精品国产欧美一区二区三区成人 | 久热免费在线 | 噜久寡妇噜噜久久寡妇 | 欧美精品一区二区三区蜜桃视频 | 久久国内| 水蜜桃久久夜色精品一区 | 精品粉嫩超白一线天av | 亚洲一区二区三区在线视频 | 久久久久亚洲精品 | 亚洲精品中文字幕在线观看 | 羞羞视频免费在线观看 | 精品美女 | 高清一区二区三区 | 色先锋影音 | 久久精品国产亚洲一区二区 | 亚洲精品视频一区二区三区 | www精品| aaa国产大片 | 国产95在线| 国产蜜臀| 伊人久久大香线 | 亚洲v区| 久久精品日产第一区二区三区 | 狠狠av | 亚洲国产日韩一区 | 成人特区 | 九九久久精品 | 国产精品久久久久久久久久久久午夜片 | 久久久久久成人 | av免费网址 | 国产一区二区欧美 | 亚洲一区二区三区四区五区午夜 | 欧美日韩视频一区二区 | 亚洲免费视频网站 |