[Python-Dev] Can ftp url start with file:// ?

Tim Lesher tlesher at gmail.com
Fri Jul 9 19:31:34 CEST 2010


On Fri, Jul 9, 2010 at 12:41, Bill Janssen <janssen at parc.com> wrote:

> So, FTP is *not* the "default protocol".  On the other hand, if <host>
> actually begins with "ftp.", it's a pretty good guess that FTP will
> work.


Actually, FTP *is* the default protocol for most URLs with hostnames in
urllib.py.

urllib.open_file() delegates to open_ftp() if there's a any host other than
the exact string "localhost", and open_local_file() otherwise.

>>> import urllib
>>> f =urllib.urlopen('file:///foo.txt')
>>> f =urllib.urlopen('file://localhost/foo.txt')
>>> f = urllib.urlopen('file://www.google.com/')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python25\lib\urllib.py", line 82, in urlopen
    return opener.open(url)
  File "c:\python25\lib\urllib.py", line 190, in open
    return getattr(self, name)(url)
  File "c:\python25\lib\urllib.py", line 457, in open_file
    return self.open_ftp(url)
  File "c:\python25\lib\urllib.py", line 538, in open_ftp
    ftpwrapper(user, passwd, host, port, dirs)
  File "c:\python25\lib\urllib.py", line 844, in __init__
    self.init()
  File "c:\python25\lib\urllib.py", line 850, in init
    self.ftp.connect(self.host, self.port)
  File "c:\python25\lib\ftplib.py", line 129, in connect
    raise socket.error, msg
IOError: [Errno ftp error] (10060, 'Operation timed out')
>>> f =urllib.urlopen('file://127.0.0.1/foo.txt')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python25\lib\urllib.py", line 82, in urlopen
    return opener.open(url)
  File "c:\python25\lib\urllib.py", line 190, in open
    return getattr(self, name)(url)
  File "c:\python25\lib\urllib.py", line 457, in open_file
    return self.open_ftp(url)
  File "c:\python25\lib\urllib.py", line 538, in open_ftp
    ftpwrapper(user, passwd, host, port, dirs)
  File "c:\python25\lib\urllib.py", line 844, in __init__
    self.init()
  File "c:\python25\lib\urllib.py", line 850, in init
    self.ftp.connect(self.host, self.port)
  File "c:\python25\lib\ftplib.py", line 129, in connect
    raise socket.error, msg
IOError: [Errno ftp error] (10061, 'Connection refused')
-- 
Tim Lesher <tlesher at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20100709/ab930241/attachment.html>


More information about the Python-Dev mailing list