You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

Fixing FTP Upload for Arrays and HTML Content

Got it, let's break down why your current code works for simple strings like "Hello World" but not for arrays or HTML code, and how to fix it. The core issue is that ftplib.storbinary() expects a valid byte stream, but your array and HTML string need proper conversion before being fed into BytesIO.

1. Uploading NumPy Arrays (like your datalog)

Arrays can't be directly converted to bytes in a human-readable format. You'll want to first convert the array to a text-based format (like CSV) so it's usable on the web server, then turn that into a byte stream. Here's how:

from ftplib import FTP
import io
import numpy as np

# Your sample array
datalog = np.array([['Temperature', 0, 0], ['Humidity', 0, 0]])

# 1. Convert array to a CSV-formatted byte stream
bio = io.BytesIO()
# Use savetxt to write the array to BytesIO, specify encoding for text compatibility
np.savetxt(bio, datalog, delimiter=',', fmt='%s', encoding='utf-8')
# Critical: Move the file pointer back to the start (otherwise FTP reads an empty stream)
bio.seek(0)

# 2. Complete FTP workflow (don't forget to connect and login)
ftp = FTP('your_web_server_address')
ftp.login('your_username', 'your_password')
# Upload as a CSV file (rename to .txt if you prefer plain text)
ftp.storbinary('STOR datalog.csv', bio)
ftp.quit()

2. Uploading HTML Code (like your html_code)

Your HTML string just needs to be encoded into bytes first. If your HTML has escaped characters like &lt;, you can optionally unescape them to get actual < > tags for proper rendering, but encoding works either way.

from ftplib import FTP
import io
import html  # For unescaping HTML entities (optional)

# Your sample HTML code with escaped entities
html_code = '&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;display here&lt;/body&gt;&lt;/html&gt;'

# Optional: Unescape entities to get real HTML tags
# html_code = html.unescape(html_code)

# 1. Convert HTML string to bytes and load into BytesIO
bio = io.BytesIO(html_code.encode('utf-8'))
bio.seek(0)  # Don't skip this step!

# 2. FTP upload
ftp = FTP('your_web_server_address')
ftp.login('your_username', 'your_password')
ftp.storbinary('STOR index.html', bio)
ftp.quit()

Key Tips to Avoid Headaches

  • Always reset the BytesIO pointer: After writing data to BytesIO, the pointer sits at the end of the stream. Calling bio.seek(0) moves it back to the start so storbinary() can read the full content.
  • Stick to UTF-8 encoding: This is the standard for web content, ensuring your text displays correctly on the server and in browsers.
  • Alternative array formats: If CSV isn't your preference, you could convert the array to a JSON string first (using json.dumps()), then encode it to bytes—great for structured data that needs parsing later.

内容的提问来源于stack exchange,提问作者etechniker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:54:14