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

Bill Janssen janssen at parc.com
Fri Jul 9 20:04:11 CEST 2010


Tim Lesher <tlesher at gmail.com> wrote:

> 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.

I just meant to point out that it's not specified that way by any RFC.
And, while FTP might have been a good default choice last century when
urllib.py was originally written, times have changed.  I'd suggest that
HTTP is a better (more likely to succeed) default choice in this century.

If we want to perpetuate these guessing heuristics, I'd suggest using
FTP if the hostname starts with "ftp.", and HTTP if the hostname starts
with "www.", and raise an error otherwise.

Bill


> >>> 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>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/janssen%40parc.com


More information about the Python-Dev mailing list