Changeset 193

Show
Ignore:
Timestamp:
2002-05-26 23:55:41 (7 years ago)
Author:
schwa
Message:
Added example for session factory as a wrapper class of ftplib.FTP .
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ftputil.txt

    r186 r193  
    152152session (which happens for every remote file that is opened; see 
    153153below). 
     154 
     155This functionality of the constructor also allows to wrap ftplib.FTP 
     156objects to do something that wouldn't be possible with the ftplib.FTP 
     157constructor alone. 
     158 
     159As an example, assume you want to connect to another than the default 
     160port but ftplib.FTP only offers this by means of its `connect` 
     161method, but not via its constructor. The solution is to provide a 
     162wrapper class: 
     163 
     164----- 
     165import ftplib 
     166import ftputil 
     167 
     168class MySession(ftplib.FTP): 
     169    def __init__(self, host, userid, password, port): 
     170        """Act like ftplib.FTP's constructor but connect to port x.""" 
     171        ftplib.FTP.__init__(self) 
     172        self.connect(host, port) 
     173        self.login(userid, password) 
     174 
     175# try not to use MySession() as factory, - use the class itself 
     176host = ftputil.FTPHost(host, userid, password, 
     177                       port=PORT, session_factory=MySession) 
     178# use `host` as usual 
     179----- 
    154180 
    155181FTPHost attributes and methods