0

我有一个带有 .gz 和文件名的包(xxxxxxxxxxxx_PARSERxxxxx.tar.gz)扩展名放置在远程目录中,比如 1.1.1.1(应该是作为变量的输入),我目前正在从机器运行脚本,比如 2.2。 2.2,我需要将包放在机器 3.3.3.3 中(应该作为变量输入),为此准备了一个脚本,但它不起作用,请帮助我,

这是我到目前为止编写的代码(不工作):

#!/usr/bin/python 
#ftp.py 

import sys 
import telnetlib 
from ftplib import FTP 
import os 

def handleDownload(block): 
    file.write(block) 
    print ".", 
hstip= raw_input('Enter the hstip: ')      #ip of the machine where packages ae placed
uid = raw_input('Enter the username: ')   #uid os the machine whr packages are placed
pwd = raw_input('Enter the user password: ') #pwd of hte machine where packages are palced
path = raw_input('Enter the path where packages are present: ') #path for the packages.
rmthstip= raw_input('Enter the hstip: ')      #ip of the machine where packages to be placed
rmtuid = raw_input('Enter the username: ')   #uid os the machine whr packages to be placed
rmtpwd = raw_input('Enter the user password: ') #pwd of hte machine where packages to be palced
cwd = os.getcwd() 
os.chdir(cwd) 
logout = 'parser files downloaded succesfully' 
tn = telnetlib.Telnet(rmtip) 
tn.read_until("login: ") 
tn.write(rmtuid + "\n") 
tn.read_until("Password:") 
tn.write(rmtpwd + "\n") 
ftp=FTP(hstip,uid,pwd) 
print 'Logging in.' 
ftp.login(uid,pwd) 
ftp.cwd(path) 
parserfile = 'find . -name "*PARSER*.gz"' 
filenm = os.system(parserfile) 
print filenm 
ftp.retrbinary(' RETR ', 'filenm', logout ) 
ftp.quit() 
tn.close() 
4

1 回答 1

0

如果您告诉我们该程序如何无法运行,那将非常有帮助。

通过快速浏览它,我会说你 perserfile 位和 filenm 使用是错误的。

  1. os.system返回一个整数,所以filenm不是文件名,而是 find 命令的存在状态。您想使用pipe = subprocess.Popen(['find', '.', '-name', '*PARSER*.gz'], stdout=subprocess.PIPEfor 循环pipe.stdout来读取命令生成的所有行

  2. ftp.retrbinary行看起来很可疑:您可能希望删除周围的引号'filenm'以使用上面找到的路径,并使用需要修复的回调函数(见下文)

  3. 在 中handleDownload,调用file.write将失败,因为您正在调用文件类而不是文件对象

  4. 最后,对我来说,在本地文件系统上执行搜索以查找远程ftp 服务器上的路径看起来很奇怪,这看起来就像您的代码正在执行的操作,除非您对本地路径有一些特殊性。或者您是否尝试使用 telnet 连接来执行远程搜索?IMO,如果 FTP 服务器允许您这样做并且您知道远程路径,则最好将 FTP 服务器上的 CWD 设置为该目录,检索内容并查找符合您要求的路径。

下载本身应重写为:

local_fobj = file('downloaded_file.gz', 'wb')
def download_cb(data):
    local_fobj.write(data)
ftp.retrbinary('RETR %s' % filenm, download_cb)
local_fobj.close()

其他杂项:

  1. cwd = os.getwd()其次os.chdir(cwd)可以安全删除
  2. rmtip没有定义,应该由rmthstip
于 2012-01-03T08:01:45.417 回答