| 1 |
# Copyright (c) Twisted Matrix Laboratories. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
|
| 5 |
""" |
| 6 |
An example of using the FTP client |
| 7 |
""" |
| 8 |
# Standard library imports |
| 9 |
from io import BytesIO |
| 10 |
|
| 11 |
from twisted.internet import reactor |
| 12 |
from twisted.internet.protocol import ClientCreator, Protocol |
| 13 |
|
| 14 |
# Twisted imports |
| 15 |
from twisted.protocols.ftp import FTPClient, FTPFileListProtocol |
| 16 |
from twisted.python import usage |
| 17 |
|
| 18 |
|
| 19 |
class BufferingProtocol(Protocol): |
| 20 |
"""Simple utility class that holds all data written to it in a buffer.""" |
| 21 |
|
| 22 |
def __init__(self): |
| 23 |
self.buffer = BytesIO() |
| 24 |
|
| 25 |
def dataReceived(self, data): |
| 26 |
self.buffer.write(data) |
| 27 |
|
| 28 |
|
| 29 |
# Define some callbacks |
| 30 |
|
| 31 |
|
| 32 |
def success(response): |
| 33 |
print("Success! Got response:") |
| 34 |
print("---") |
| 35 |
if response is None: |
| 36 |
print(None) |
| 37 |
else: |
| 38 |
print("\n".join(response)) |
| 39 |
print("---") |
| 40 |
|
| 41 |
|
| 42 |
def fail(error): |
| 43 |
print("Failed. Error was:") |
| 44 |
print(error) |
| 45 |
|
| 46 |
|
| 47 |
def showFiles(result, fileListProtocol): |
| 48 |
print("Processed file listing:") |
| 49 |
for file in fileListProtocol.files: |
| 50 |
print( |
| 51 |
" {}: {} bytes, {}".format(file["filename"], file["size"], file["date"]) |
| 52 |
) |
| 53 |
print(f"Total: {len(fileListProtocol.files)} files") |
| 54 |
|
| 55 |
|
| 56 |
def showBuffer(result, bufferProtocol): |
| 57 |
print("Got data:") |
| 58 |
print(bufferProtocol.buffer.getvalue()) |
| 59 |
|
| 60 |
|
| 61 |
class Options(usage.Options): |
| 62 |
optParameters = [ |
| 63 |
["host", "h", "localhost"], |
| 64 |
["port", "p", 21], |
| 65 |
["username", "u", "anonymous"], |
| 66 |
["password", None, "twisted@"], |
| 67 |
["passive", None, 0], |
| 68 |
["debug", "d", 1], |
| 69 |
] |
| 70 |
|
| 71 |
|
| 72 |
def run(): |
| 73 |
# Get config |
| 74 |
config = Options() |
| 75 |
config.parseOptions() |
| 76 |
config.opts["port"] = int(config.opts["port"]) |
| 77 |
config.opts["passive"] = int(config.opts["passive"]) |
| 78 |
config.opts["debug"] = int(config.opts["debug"]) |
| 79 |
|
| 80 |
# Create the client |
| 81 |
FTPClient.debug = config.opts["debug"] |
| 82 |
creator = ClientCreator( |
| 83 |
reactor, |
| 84 |
FTPClient, |
| 85 |
config.opts["username"], |
| 86 |
config.opts["password"], |
| 87 |
passive=config.opts["passive"], |
| 88 |
) |
| 89 |
creator.connectTCP(config.opts["host"], config.opts["port"]).addCallback( |
| 90 |
connectionMade |
| 91 |
).addErrback(connectionFailed) |
| 92 |
reactor.run() |
| 93 |
|
| 94 |
|
| 95 |
def connectionFailed(f): |
| 96 |
print("Connection Failed:", f) |
| 97 |
reactor.stop() |
| 98 |
|
| 99 |
|
| 100 |
def connectionMade(ftpClient): |
| 101 |
# Get the current working directory |
| 102 |
ftpClient.pwd().addCallbacks(success, fail) |
| 103 |
|
| 104 |
# Get a detailed listing of the current directory |
| 105 |
fileList = FTPFileListProtocol() |
| 106 |
d = ftpClient.list(".", fileList) |
| 107 |
d.addCallbacks(showFiles, fail, callbackArgs=(fileList,)) |
| 108 |
|
| 109 |
# Change to the parent directory |
| 110 |
ftpClient.cdup().addCallbacks(success, fail) |
| 111 |
|
| 112 |
# Create a buffer |
| 113 |
proto = BufferingProtocol() |
| 114 |
|
| 115 |
# Get short listing of current directory, and quit when done |
| 116 |
d = ftpClient.nlst(".", proto) |
| 117 |
d.addCallbacks(showBuffer, fail, callbackArgs=(proto,)) |
| 118 |
d.addCallback(lambda result: reactor.stop()) |
| 119 |
|
| 120 |
|
| 121 |
# this only runs if the module was *not* imported |
| 122 |
if __name__ == "__main__": |
| 123 |
run() |