使用FTPlib上载200kb的html页面

2024-05-21 05:39:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我用下面的代码来上传一个html文件到我的网站,但是当上传时它似乎丢失了一些数据。。。在

我的内容是1930行,“长度”为298872(我猜这是多少个字符)

## Login using the ftplib library and set the session as the variable ftp_session
ftp_session = ftplib.FTP('ftp.website.com','admin@website.com','password')

## Open a file to upload
html_ftp_file = open('OUTPUT/output.html','rb')
## Open a folder under in the ftp server
ftp_session.cwd("/folder")
## Send/upload the the binary code of said file to the ftp server
ftp_session.storbinary('STOR output.html', html_ftp_file )
## Close the ftp_file
html_ftp_file.close()

## Quit out of the FTP session
ftp_session.quit()

为什么不能100%上传文件?它正在上传98%的内容。。。在

我一直在四处寻找,但找不到最大字符限制或最大文件大小是多少,是否可以通过部分上传来修复此问题(我不确定如何做到这一点)

被动

^{pr2}$

主动

*cmd* 'CWD /folder'
*resp* '250 OK. Current directory is /folder'
*cmd* 'TYPE A'
*resp* '200 TYPE is now ASCII'
*cmd* 'PORT xxx,xxx,xxx,xxx,203,212'
*resp* '200 PORT command successful'
*cmd* 'STOR output.html'
*resp* '150 Connecting to port 52180'
*resp* '226-File successfully transferred'
*resp* '226 4.102 seconds (measured here), 38.03 Kbytes per second'
*cmd* 'QUIT'
*resp* '221-Goodbye. You uploaded 157 and downloaded 0 kbytes.'
*resp* '221 Logout.'

Tags: and文件thetocmd内容outputsession
2条回答

尝试以下操作,它将以二进制模式上载文件。诀窍是在调用storbinary之前将文件传输的类型设置为二进制模式(类型I)。在

with open('OUTPUT/output.html','rb') as html_ftp_file:
    # ftp_session.set_pasv(1) # If you want to use passive mode for transfer.
    ftp_session.voidcmd('TYPE I')  # Set file transfer type to Image (binary)
    ftp_session.cwd("/folder")
    ftp_session.storbinary('STOR output.html', html_ftp_file)

从你的代码看来,你的FTP模式是二进制的,但是你上传的是一个ASCII文件(html)。尝试将FTP模式更改为ASCII或压缩您的文件(这将是一个二进制文件),发送它,然后在目的地解压缩。在

这是来自http://effbot.org/librarybook/ftplib.htm的邮件

import ftp
import os

def upload(ftp, file):
    ext = os.path.splitext(file)[1]
    if ext in (".txt", ".htm", ".html"):
        ftp.storlines("STOR " + file, open(file))
    else:
        ftp.storbinary("STOR " + file, open(file, "rb"), 1024)

ftp = ftplib.FTP("ftp.fbi.gov")
ftp.login("mulder", "trustno1")

upload(ftp, "trixie.zip")
upload(ftp, "file.txt")
upload(ftp, "sightings.jpg")

相关问题 更多 >