FTP Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsSystem AdministrationFTP Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old February 16th, 2004, 03:35 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ftplib

I quickly wrote a simple ftp client using the ftplib, but I'm not quite sure how to get it to download a file. What it said in the Python docs wasn't really clear to me.

Reply With Quote
  #2  
Old February 16th, 2004, 06:15 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Surly it would be much easier to use urllib to download a file and just use ftplib for uploads? Just a thought . Interesting though, any more info you could provide would be good.

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old February 16th, 2004, 07:13 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old February 17th, 2004, 05:19 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I dont see why it should if you have the path to the file you want to download and the FTP server isnt password protected (not sure if that'll make a differance but).

Sadly what you're probably looking for is somthing like curses. Which are anything but cross platform right now. You're best bet would be to do something like this...

Code:
>>> import sys, os
>>> sys.platform
'win32'
>>> if sys.platform == 'win32':
...     os.system('cls')
... else:
...     os.system('clear')
...     
0
>>> 


Mark.

Reply With Quote
  #5  
Old February 17th, 2004, 03:54 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I don't think the urllib is what I need, but I found the proper function in ftplib. It's retrbinary()

Something like this...
Code:
def callbackfunc(self, var):
   do something with data....

retrbinary('RETR filename', self.callbackfunc)


I need to mess around a bit with it, atm it only prints what it downloads to the screen, but it shouldn't be hard .

Last edited by XxChris : February 17th, 2004 at 04:02 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsSystem AdministrationFTP Help > ftplib


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway