Mailing List Archive

HELP - FTP seesions using python????????
any links or tips etc on how to tackle automation of FTP sessions using python
would be most appreciated.



-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
HELP - FTP seesions using python???????? [ In reply to ]
In article <7g328l$hga$1@nnrp1.dejanews.com>, <gony@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!
HELP - FTP seesions using python???????? [ In reply to ]
In article <7g328l$hga$1@nnrp1.dejanews.com>,
gony@my-dejanews.com wrote:
> any links or tips etc on how to tackle automation of FTP sessions using python
> would be most appreciated.
Here is a batch example, it is an extract so it comes with no warranty :)
It implements a 'put'-Operation on a file from Windows NT to an IBM-Host


import ftplib

def doit():
ftp = ftplib.FTP(FTPNameHost)
ftp.login(FTPUserId,FTPPasswort)
fileA= open(AuftragSammelDateiName,'r')
ftp.storlines("STOR '" + AuftragHostDateiname + "'" ,fileA)
ftp.quit()
fileA.close()

The 'STOR' and the "'" - Delimiters are specific for
the FTP-Implementation on an IBM Host.

Hope that helps !
--
Norbert Klamann
Klamann Software & Beratung
Erftstadt Germany
Klamann.Software@pobox.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
HELP - FTP seesions using python???????? [ In reply to ]
thanks for the samples

I'll work my way through them and figure out what they do and how they do it

another small question in perl the #!/usr/local/bin/perl is not needed when
run on a windows machine, does it matter for Python on a windows machine
whether this line is present or not?


In article <aahzFAtrsr.L3w@netcom.com>,
aahz@netcom.com (Aahz Maruch) wrote:
> In article <7g328l$hga$1@nnrp1.dejanews.com>, <gony@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!
>

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
HELP - FTP seesions using python???????? [ In reply to ]
In article <7g5ueh$3pt$1@nnrp1.dejanews.com>, <gony@my-dejanews.com> wrote:
>
>another small question in perl the #!/usr/local/bin/perl is not needed when
>run on a windows machine, does it matter for Python on a windows machine
>whether this line is present or not?

No, but if you set Windows up to recognize .py as an executable
extension, you can leave that line in there and have a
platform--independent program.
--
--- 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!