retrieve data using FTP in Python

Steve Holden steve at holdenweb.com
Wed Sep 14 01:51:52 EDT 2005


swarna pulavarty wrote:
> Hi all,
>  
> I am new to this Python group and to Python . I need to retrieve data 
> from an arbitrary URL and save it to a file.
> Can anyone tell me how to retrieve "any" data using FTP modules in 
> Python ? And also, Can you suggest me some books and online references 
> to get familiar with Python and especially FTP modules in Python ? 
>  
> Your help is appreciated !
>  
Swana:

If you need to handle arbitrary URLs then you need either urllib or 
urllib2, both components of the standard library. See

   http://docs.python.org/lib/module-urllib.html and
   http://docs.python.org/lib/module-urllib2.html

I don't use urllib2 much myself, so here's an example from urllib. How 
much easier can this be?

$ python
Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
  >>> import urllib
  >>> f = urllib.urlopen("http://www.holdenweb.com/")
  >>> dir(f)
['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'close', 
'fileno',
  'fp', 'geturl', 'headers', 'info', 'next', 'read', 'readline', 
'readlines', 'ur
l']
  >>> print f.read()
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   ...
</div>
</body>
</html>

  >>> f.headers.keys()
['content-length', 'accept-ranges', 'server', 'last-modified', 
'connection', 'etag', 'date', 'content-type']
  >>> f.headers["content-length"]
'13378'
  >>>

Good luck!

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list