| | 154 | |
|---|
| | 155 | This functionality of the constructor also allows to wrap ftplib.FTP |
|---|
| | 156 | objects to do something that wouldn't be possible with the ftplib.FTP |
|---|
| | 157 | constructor alone. |
|---|
| | 158 | |
|---|
| | 159 | As an example, assume you want to connect to another than the default |
|---|
| | 160 | port but ftplib.FTP only offers this by means of its `connect` |
|---|
| | 161 | method, but not via its constructor. The solution is to provide a |
|---|
| | 162 | wrapper class: |
|---|
| | 163 | |
|---|
| | 164 | ----- |
|---|
| | 165 | import ftplib |
|---|
| | 166 | import ftputil |
|---|
| | 167 | |
|---|
| | 168 | class 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 |
|---|
| | 176 | host = ftputil.FTPHost(host, userid, password, |
|---|
| | 177 | port=PORT, session_factory=MySession) |
|---|
| | 178 | # use `host` as usual |
|---|
| | 179 | ----- |
|---|