From: aahz at netcom.com (Aahz Maruch)
Date: Tue, 27 Apr 1999 01:50:51 GMT
Subject: HELP - FTP seesions using python????????
References: <7g328l$hga$1@nnrp1.dejanews.com>
Message-ID: <aahzFAtrsr.L3w@netcom.com>
Content-Length: 1877
X-UID: 676                                                  

In article <7g328l$hga$1 at nnrp1.dejanews.com>,  <gony at my-dejanews.com> wrote:
>
>any links or tips etc on how to tackle automation of FTP sessions using python
>would be most appreciated.

Here's a sample; there aren't any comments (proof that Python can be
almost as obscure as Perl ;-), but it shouldn't be *that* hard to read:

#!/usr/local/bin/python

import sys, os, ftplib, string

def getpass(prompt = "Password: "):
    import termios, TERMIOS, sys
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd) 
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~TERMIOS.ECHO          # lflags
    try:
        termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new)
        passwd = raw_input(prompt)
    finally:
        termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old)
	print
    return passwd



if len ( sys.argv ) < 4 :
	raise 'Must have three arguments: host[:path], user[:passwd], and file'

HostPath = sys.argv[1]
pos = string.find ( HostPath, ':' )
if pos > 0 :
	host = HostPath[0:pos]
	if ( pos + 1 ) == len ( HostPath ) :
		path = ""
	else:
		path = HostPath[(pos+1):]
else :
	host = HostPath
	path = ""


UserPass = sys.argv[2]
pos = string.find ( UserPass, ':' )
if pos > 0 :
	user = UserPass[0:pos]
	if ( pos + 1 ) == len ( UserPass ) :
		passwd = ""
	else:
		passwd = UserPass[(pos+1):]
else :
	user = UserPass
	passwd = getpass()


filename = sys.argv[3]
pos = string.rfind ( filename, '/' )
if pos > 0 :
	basename = filename[pos:]
else :
	basename = filename


fd = open ( filename )
ftp = ftplib.FTP ( host, user, passwd )
ftp.cwd ( path )
ftp.storbinary ( 'STOR %s' % basename, fd, 1024 * 16 )
ftp.close()

fd.close()
-- 
                      --- Aahz (@netcom.com)

Hugs and backrubs -- I break Rule 6       <*>      http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het

Hi!  I'm a beta for SigVirus 2000!  Copy me into your .sigfile!




