FTP';使用Python 3的d文本文件为空
我正在尝试将一个文件从运行Python3.3的windows设备中FTP到运行Ubuntu12.04的linux设备中 我已经能够通过FTP传输该文件,但远程服务器上的文件始终为空,即使本地文件中约有8k。它是一个小的文本文件 我尝试了谷歌和SE网络的一些建议。所有不同的测试都被注释掉了。这些测试失败,出现了各种错误和/或与未注释测试类似的结果。(0字节文件) 我还读取了txt文件,以确保我正在FTPing正确的文件 代码如下。如有任何帮助,将不胜感激:FTP';使用Python 3的d文本文件为空,python,python-3.x,ftp,Python,Python 3.x,Ftp,我正在尝试将一个文件从运行Python3.3的windows设备中FTP到运行Ubuntu12.04的linux设备中 我已经能够通过FTP传输该文件,但远程服务器上的文件始终为空,即使本地文件中约有8k。它是一个小的文本文件 我尝试了谷歌和SE网络的一些建议。所有不同的测试都被注释掉了。这些测试失败,出现了各种错误和/或与未注释测试类似的结果。(0字节文件) 我还读取了txt文件,以确保我正在FTPing正确的文件 代码如下。如有任何帮助,将不胜感激: filename = 'Music.tx
filename = 'Music.txt'
#write data to file
file = open('Music.txt', 'w+')
for music in musicList:
file.write(music+'\n')
file.close()
localfile = open(filename, 'r')
#print(localfile)
#for line in localfile:
# print(line)
#ftp file to webserver for consumption
ftp = FTP(host) # connect to host webdev (local)
ftp.login(username, password)
print(ftp.getwelcome())
ftp.cwd('projects/home/musicCat/data')
ftp.retrbinary("RETR Music.txt", localfile.write)
#ftp.storlines("STOR Music.txt", open('Music.txt'))
#ftp.storbinary("STOR Music.txt", localfile.write)
#ftp.storbinary("STOR Music.txt", file(file, "rb"))
#ftp.storbinary('STOR' + localfile.name, open(file.name, 'wb').write)
#ftp.storlines("STOR Music.txt", open(localfile, 'r'))
#ftp.storlines('STOR Music.txt', file)
print(ftp.retrlines('LIST'))
ftp.quit()
解释器输出:
220 (vsFTPd 2.3.5)
-rw-r--r-- 1 1000 1000 0 Dec 27 21:14 Music.txt
226 Directory send OK.
>>>
有人有什么想法吗?我还没有能够成功地通过ftp传输文件。我正在讨论如何使用Python2.7和paramiko 我确信您正在寻找一种跨平台的纯python方法,但有时使用python通过
subprocess.call()执行外部实用程序是最简单的
我在从Windows建立SSH隧道时遇到了类似的问题,我真的不想切换到2.7来使用paramiko。我最终只是从脚本中调用了plink.exe
对于Windows上的ftp,您可以使用内置实用程序ftp
Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.
FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer] [-r:recvbuffer] [-b:asyncbuffers] [-w:windowsize] [host]
-v Suppresses display of remote server responses.
-n Suppresses auto-login upon initial connection.
-i Turns off interactive prompting during multiple file
transfers.
-d Enables debugging.
-g Disables filename globbing (see GLOB command).
-s:filename Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
-a Use any local interface when binding data connection.
-A login as anonymous.
-x:send sockbuf Overrides the default SO_SNDBUF size of 8192.
-r:recv sockbuf Overrides the default SO_RCVBUF size of 8192.
-b:async count Overrides the default async count of 3
-w:windowsize Overrides the default transfer buffer size of 65535.
host Specifies the host name or IP address of the remote
host to connect to.
Notes:
- mget and mput commands take y/n/q for yes/no/quit.
- Use Control-C to abort commands.
如果可能的话,我仍在尝试用python来完成这一切。但随着时间的推移,我可能不得不采用不同的方法,2.7和paramiko或您的建议plink。我最终只是将数据发布到和端点,端点通过php访问服务器并能够保存数据。接下来,我计划使用上面详细介绍的方法,调用特定于操作系统的程序来处理ftp传输。@RickyHewitt感谢您的跟进。