Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
ftplib Namespace Reference

Data Structures

class  Error
 
class  error_reply
 
class  error_temp
 
class  error_perm
 
class  error_proto
 
class  FTP
 
class  Netrc
 

Functions

def parse150
 
def parse227
 
def parse229
 
def parse257
 
def print_line
 
def ftpcp
 
def test
 

Variables

list __all__ = ["FTP","Netrc"]
 
int MSG_OOB = 0x1
 
int FTP_PORT = 21
 
tuple all_errors = (Error, socket.error, IOError, EOFError)
 
string CRLF = '\r\n'
 
 _150_re = None
 
 _227_re = None
 

Detailed Description

An FTP client class and some helper functions.

Based on RFC 959: File Transfer Protocol (FTP), by J. Postel and J. Reynolds

Example:

>>> from ftplib import FTP
>>> ftp = FTP('ftp.python.org') # connect to host, default port
>>> ftp.login() # default, i.e.: user anonymous, passwd user@hostname
'230 Guest login ok, access restrictions apply.'
>>> ftp.retrlines('LIST') # list directory contents
total 9
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 .
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 ..
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 bin
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 etc
d-wxrwxr-x   2 ftp      wheel        1024 Sep  5 13:43 incoming
drwxr-xr-x   2 root     wheel        1024 Nov 17  1993 lib
drwxr-xr-x   6 1094     wheel        1024 Sep 13 19:07 pub
drwxr-xr-x   3 root     wheel        1024 Jan  3  1994 usr
-rw-r--r--   1 root     root          312 Aug  1  1994 welcome.msg
'226 Transfer complete.'
>>> ftp.quit()
'221 Goodbye.'
>>>

A nice test that reveals some of the network dialogue would be:
python ftplib.py -d localhost -l -p -l

Function Documentation

def ftplib.ftpcp (   source,
  sourcename,
  target,
  targetname = '',
  type = 'I' 
)
Copy file from one FTP-instance to another.

Definition at line 636 of file ftplib.py.

References parse227().

637 def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
638  '''Copy file from one FTP-instance to another.'''
639  if not targetname: targetname = sourcename
640  type = 'TYPE ' + type
641  source.voidcmd(type)
642  target.voidcmd(type)
643  sourcehost, sourceport = parse227(source.sendcmd('PASV'))
644  target.sendport(sourcehost, sourceport)
645  # RFC 959: the user must "listen" [...] BEFORE sending the
646  # transfer request.
647  # So: STOR before RETR, because here the target is a "user".
648  treply = target.sendcmd('STOR ' + targetname)
649  if treply[:3] not in ('125', '150'): raise error_proto # RFC 959
650  sreply = source.sendcmd('RETR ' + sourcename)
651  if sreply[:3] not in ('125', '150'): raise error_proto # RFC 959
652  source.voidresp()
653  target.voidresp()
654 
def ftplib.parse150 (   resp)
Parse the '150' response for a RETR request.
Returns the expected transfer size or None; size is not guaranteed to
be present in the 150 message.

Definition at line 543 of file ftplib.py.

544 def parse150(resp):
545  '''Parse the '150' response for a RETR request.
546  Returns the expected transfer size or None; size is not guaranteed to
547  be present in the 150 message.
548  '''
549  if resp[:3] != '150':
550  raise error_reply, resp
551  global _150_re
552  if _150_re is None:
553  import re
554  _150_re = re.compile("150 .* \((\d+) bytes\)", re.IGNORECASE)
555  m = _150_re.match(resp)
556  if not m:
557  return None
558  s = m.group(1)
559  try:
560  return int(s)
561  except (OverflowError, ValueError):
562  return long(s)
563 
def ftplib.parse227 (   resp)
Parse the '227' response for a PASV request.
Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)'
Return ('host.addr.as.numbers', port#) tuple.

Definition at line 566 of file ftplib.py.

References dospath.join().

567 def parse227(resp):
568  '''Parse the '227' response for a PASV request.
569  Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)'
570  Return ('host.addr.as.numbers', port#) tuple.'''
571 
572  if resp[:3] != '227':
573  raise error_reply, resp
574  global _227_re
575  if _227_re is None:
576  import re
577  _227_re = re.compile(r'(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)')
578  m = _227_re.search(resp)
579  if not m:
580  raise error_proto, resp
581  numbers = m.groups()
582  host = '.'.join(numbers[:4])
583  port = (int(numbers[4]) << 8) + int(numbers[5])
584  return host, port
585 
def ftplib.parse229 (   resp,
  peer 
)
Parse the '229' response for a EPSV request.
Raises error_proto if it does not contain '(|||port|)'
Return ('host.addr.as.numbers', port#) tuple.

Definition at line 586 of file ftplib.py.

References string.atoi(), string.find(), and string.split().

587 def parse229(resp, peer):
588  '''Parse the '229' response for a EPSV request.
589  Raises error_proto if it does not contain '(|||port|)'
590  Return ('host.addr.as.numbers', port#) tuple.'''
591 
592  if resp[:3] <> '229':
593  raise error_reply, resp
594  left = string.find(resp, '(')
595  if left < 0: raise error_proto, resp
596  right = string.find(resp, ')', left + 1)
597  if right < 0:
598  raise error_proto, resp # should contain '(|||port|)'
599  if resp[left + 1] <> resp[right - 1]:
600  raise error_proto, resp
601  parts = string.split(resp[left + 1:right], resp[left+1])
602  if len(parts) <> 5:
603  raise error_proto, resp
604  host = peer[0]
605  port = string.atoi(parts[3])
606  return host, port
607 
def ftplib.parse257 (   resp)
Parse the '257' response for a MKD or PWD request.
This is a response to a MKD or PWD request: a directory name.
Returns the directoryname in the 257 reply.

Definition at line 608 of file ftplib.py.

609 def parse257(resp):
610  '''Parse the '257' response for a MKD or PWD request.
611  This is a response to a MKD or PWD request: a directory name.
612  Returns the directoryname in the 257 reply.'''
613 
614  if resp[:3] != '257':
615  raise error_reply, resp
616  if resp[3:5] != ' "':
617  return '' # Not compliant to RFC 959, but UNIX ftpd does this
618  dirname = ''
619  i = 5
620  n = len(resp)
621  while i < n:
622  c = resp[i]
623  i = i+1
624  if c == '"':
625  if i >= n or resp[i] != '"':
626  break
627  i = i+1
628  dirname = dirname + c
629  return dirname
630 
def ftplib.print_line (   line)
Default retrlines callback to print a line.

Definition at line 631 of file ftplib.py.

632 def print_line(line):
633  '''Default retrlines callback to print a line.'''
634  print line
635 
def ftplib.test ( )
Test program.
Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...

Definition at line 762 of file ftplib.py.

763 def test():
764  '''Test program.
765  Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...'''
766 
767  debugging = 0
768  rcfile = None
769  while sys.argv[1] == '-d':
770  debugging = debugging+1
771  del sys.argv[1]
772  if sys.argv[1][:2] == '-r':
773  # get name of alternate ~/.netrc file:
774  rcfile = sys.argv[1][2:]
775  del sys.argv[1]
776  host = sys.argv[1]
777  ftp = FTP(host)
778  ftp.set_debuglevel(debugging)
779  userid = passwd = acct = ''
780  try:
781  netrc = Netrc(rcfile)
782  except IOError:
783  if rcfile is not None:
784  sys.stderr.write("Could not open account file"
785  " -- using anonymous login.")
786  else:
787  try:
788  userid, passwd, acct = netrc.get_account(host)
789  except KeyError:
790  # no account for host
791  sys.stderr.write(
792  "No account -- using anonymous login.")
793  ftp.login(userid, passwd, acct)
794  for file in sys.argv[2:]:
795  if file[:2] == '-l':
796  ftp.dir(file[2:])
797  elif file[:2] == '-d':
798  cmd = 'CWD'
799  if file[2:]: cmd = cmd + ' ' + file[2:]
800  resp = ftp.sendcmd(cmd)
801  elif file == '-p':
802  ftp.set_pasv(not ftp.passiveserver)
803  else:
804  ftp.retrbinary('RETR ' + file, \
805  sys.stdout.write, 1024)
806  ftp.quit()
807 

Variable Documentation

_150_re = None

Definition at line 541 of file ftplib.py.

_227_re = None

Definition at line 564 of file ftplib.py.

list __all__ = ["FTP","Netrc"]

Definition at line 48 of file ftplib.py.

tuple all_errors = (Error, socket.error, IOError, EOFError)

Definition at line 68 of file ftplib.py.

string CRLF = '\r\n'

Definition at line 72 of file ftplib.py.

int FTP_PORT = 21

Definition at line 55 of file ftplib.py.

int MSG_OOB = 0x1

Definition at line 51 of file ftplib.py.