
February 16th, 2004, 07:13 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Would urllib let me download a file from an ftp server? I take a look on the Python site.
Btw, here is the code for anyone curious, although don't expect much as I wrote it quickly and havn't even looked over it yet, BUT it works  .
Code:
#!/usr/bin/env python
#
# simpleFTP - Written by c.scott
#
import ftplib
import sys
import os
class commandList:
def info(self, command):
usage = { 'connect' : 'usage: connect <server> <username> <password>',
'send' : 'usage: send <filepath / filename>',
'del' : 'usage: del <filename>' }
if command == 'all':
print '\n'
for cmd in usage.keys():
print cmd + ':'
print '\t' + usage[cmd]
else:
print usage[command]
class ftpDriver:
def __init__(self):
os.system('clear')
print "\nsimpleFTP - Written by c.scott\n"
self.server = None
self.fh = None
#
# Connection Management.
#
def connect(self, server, user, pwd):
print 'connecting to %s...' % server
self.server = ftplib.FTP(server, user, pwd)
self.server.set_debuglevel(0)
self.server.getwelcome()
print 'connected'
def disconnect(self):
print 'disconnecting...'
self.server.quit()
print 'disconnected'
def exit(self):
if self.server:
self.disconnect()
if self.fh:
self.fh.close()
sys.exit()
#
# File Commands.
#
def send(self, filename):
try:
self.fh = open(filename, 'r')
except:
print '<file does not exist>'
return
try:
self.delete(filename)
except:
pass
print 'sending %s...' % filename
self.server.storbinary('STOR ' + filename, self.fh)
print 'sent'
def delete(self, filename):
try:
self.server.delete(filename)
except ftplib.error_perm:
print 'filename %s doesn\'t exist' % filename
def rename(self, filename, toname):
self.server.rename(filename, toname)
def size(self, filename):
self.server.size(filename)
def get(self, filename):
self.server.retrbinary('RETR ' + filename)
#
# Directory Commands.
#
def delDir(self, filename):
self.server.rmd(filename)
def makeDir(self, filename):
self.server.mkd(filename)
def changeDir(self, directory):
self.server.cwd(directory)
def listDir(self):
self.server.retrlines('LIST')
def currentDir(self):
print self.server.pwd()
class ftpClient(ftpDriver, commandList):
def parse(self, input):
input = input.split()
command = input[0]
del input[0]
if command == 'connect':
if len(input) >= 3:
self.connect(input[0], input[1], input[2])
else:
self.info('connect')
if command == 'disconnect':
self.disconnect()
elif command == 'send':
if len(input) >= 1:
self.send(input[0])
else:
self.info('send')
elif command == 'rm':
if len(input) >= 1:
self.delete(input[0])
else:
self.info('del')
elif command == 'rename':
if len(input) >= 2:
self.rename(input[0], input[1])
elif command == 'get':
if len(input) >= 1:
self.get(input[0])
elif command == 'mkdir':
if len(input) >= 1:
self.makeDir(input[0])
elif command == 'rmdir':
if len(input) >= 1:
self.delDir(input[0])
elif command == 'cd':
if len(input) >= 1:
self.changeDir(input[0])
elif command == 'ls':
self.listDir()
elif command == 'pwd':
self.currentDir()
elif command == 'size':
if len(input) >= 1:
self.size(input[0])
elif command == 'help':
self.info('all')
elif command == 'quit':
self.exit()
if __name__ == '__main__':
if len(sys.argv) == 4:
ftp = ftpClient()
ftp.connect(sys.argv[1], sys.argv[2], sys.argv[3])
else:
ftp = ftpClient()
while True:
input = raw_input('> ')
if input:
ftp.parse(input)
Note: the line "os.system('clear')" won't work on Windows I don't think as the DOS command to clear the screen is 'cls'. I wasn't sure if there was an OS independent clear screen method.
Last edited by XxChris : February 16th, 2004 at 07:17 PM.
|