使用FTPlib上传200kb html页面
作者:互联网
我正在使用以下代码将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%…
我一直在四处张望,无法找到最大字符数限制或最大文件大小,是否可以通过分批上传来解决(我不确定该怎么做)
被动
*cmd* 'CWD /folder'
*resp* '250 OK. Current directory is /folder'
*cmd* 'TYPE A'
*resp* '200 TYPE is now ASCII'
*cmd* 'PASV'
*resp* '227 Entering Passive Mode (xxx,xxx,xxx,xxx,73,19)'
*cmd* 'STOR output.html'
*resp* '150 Accepted data connection'
*resp* '226-File successfully transferred'
*resp* '226 3.235 seconds (measured here), 48.23 Kbytes per second'
*cmd* 'QUIT'
*resp* '221-Goodbye. You uploaded 157 and downloaded 0 kbytes.'
*resp* '221 Logout.'
活性
*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.'
解决方法:
从您的代码看来,您的FTP模式是二进制模式,但是您正在上载ASCII文件(html).尝试将FTP模式更改为ASCII或先压缩文件(该文件将是二进制文件),然后将其发送然后在目标位置解压缩.
这是http://effbot.org/librarybook/ftplib.htm的eaxmple
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")
标签:python-2-7,ftplib,python 来源: https://codeday.me/bug/20191122/2060439.html