Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a79449e

Browse files
committed
Patch #711838: Allow non-anonymous ftp urls in urllib2.
Backported to 2.3.
1 parent d3f4a1a commit a79449e

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

Lib/urllib2.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
# not sure how many of these need to be gotten rid of
116116
from urllib import unwrap, unquote, splittype, splithost, \
117117
addinfourl, splitport, splitgophertype, splitquery, \
118-
splitattr, ftpwrapper, noheaders
118+
splitattr, ftpwrapper, noheaders, splituser, splitpasswd
119119

120120
# support for FileHandler, proxies via environment variables
121121
from urllib import localhost, url2pathname, getproxies
@@ -1090,21 +1090,30 @@ def ftp_open(self, req):
10901090
host = req.get_host()
10911091
if not host:
10921092
raise IOError, ('ftp error', 'no host given')
1093-
# XXX handle custom username & password
1093+
host, port = splitport(host)
1094+
if port is None:
1095+
port = ftplib.FTP_PORT
1096+
1097+
# username/password handling
1098+
user, host = splituser(host)
1099+
if user:
1100+
user, passwd = splitpasswd(user)
1101+
else:
1102+
passwd = None
1103+
host = unquote(host)
1104+
user = unquote(user or '')
1105+
passwd = unquote(passwd or '')
1106+
10941107
try:
10951108
host = socket.gethostbyname(host)
10961109
except socket.error, msg:
10971110
raise URLError(msg)
1098-
host, port = splitport(host)
1099-
if port is None:
1100-
port = ftplib.FTP_PORT
11011111
path, attrs = splitattr(req.get_selector())
11021112
dirs = path.split('/')
11031113
dirs = map(unquote, dirs)
11041114
dirs, file = dirs[:-1], dirs[-1]
11051115
if dirs and not dirs[0]:
11061116
dirs = dirs[1:]
1107-
user = passwd = '' # XXX
11081117
try:
11091118
fw = self.connect_ftp(user, passwd, host, port, dirs)
11101119
type = file and 'I' or 'D'

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ Extension modules
237237
Library
238238
-------
239239

240+
- Support non-anonymous ftp URLs in urllib2.
241+
240242
- The encodings package will now applies codec name aliases
241243
first before starting to try the import of the codec module.
242244
This simplifies overriding built-in codecs with external

0 commit comments

Comments
 (0)