~vishvananda/nova/network-refactor

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/examples/ftpclient.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

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