Patch #711838: Allow non-anonymous ftp urls in urllib2.

Backported to 2.3.
This commit is contained in:
Martin v. Löwis 2004-02-15 21:19:18 +00:00
parent d3f4a1a00a
commit a79449e7a2
2 changed files with 17 additions and 6 deletions

View file

@ -115,7 +115,7 @@ except ImportError:
# not sure how many of these need to be gotten rid of # not sure how many of these need to be gotten rid of
from urllib import unwrap, unquote, splittype, splithost, \ from urllib import unwrap, unquote, splittype, splithost, \
addinfourl, splitport, splitgophertype, splitquery, \ addinfourl, splitport, splitgophertype, splitquery, \
splitattr, ftpwrapper, noheaders splitattr, ftpwrapper, noheaders, splituser, splitpasswd
# support for FileHandler, proxies via environment variables # support for FileHandler, proxies via environment variables
from urllib import localhost, url2pathname, getproxies from urllib import localhost, url2pathname, getproxies
@ -1090,21 +1090,30 @@ class FTPHandler(BaseHandler):
host = req.get_host() host = req.get_host()
if not host: if not host:
raise IOError, ('ftp error', 'no host given') raise IOError, ('ftp error', 'no host given')
# XXX handle custom username & password host, port = splitport(host)
if port is None:
port = ftplib.FTP_PORT
# username/password handling
user, host = splituser(host)
if user:
user, passwd = splitpasswd(user)
else:
passwd = None
host = unquote(host)
user = unquote(user or '')
passwd = unquote(passwd or '')
try: try:
host = socket.gethostbyname(host) host = socket.gethostbyname(host)
except socket.error, msg: except socket.error, msg:
raise URLError(msg) raise URLError(msg)
host, port = splitport(host)
if port is None:
port = ftplib.FTP_PORT
path, attrs = splitattr(req.get_selector()) path, attrs = splitattr(req.get_selector())
dirs = path.split('/') dirs = path.split('/')
dirs = map(unquote, dirs) dirs = map(unquote, dirs)
dirs, file = dirs[:-1], dirs[-1] dirs, file = dirs[:-1], dirs[-1]
if dirs and not dirs[0]: if dirs and not dirs[0]:
dirs = dirs[1:] dirs = dirs[1:]
user = passwd = '' # XXX
try: try:
fw = self.connect_ftp(user, passwd, host, port, dirs) fw = self.connect_ftp(user, passwd, host, port, dirs)
type = file and 'I' or 'D' type = file and 'I' or 'D'

View file

@ -237,6 +237,8 @@ Extension modules
Library Library
------- -------
- Support non-anonymous ftp URLs in urllib2.
- The encodings package will now applies codec name aliases - The encodings package will now applies codec name aliases
first before starting to try the import of the codec module. first before starting to try the import of the codec module.
This simplifies overriding built-in codecs with external This simplifies overriding built-in codecs with external