diff options
author | Guido van Rossum <guido@python.org> | 1998-07-22 21:33:23 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-07-22 21:33:23 (GMT) |
commit | 810a3396d130eda7d3e2be08966b40949757598e (patch) | |
tree | a41338a1bb83029301f3b294e1bf562062ae4dd6 /Lib | |
parent | d81fc3cd64ff77e9c362b5a5fa09db21a089b691 (diff) | |
download | cpython-810a3396d130eda7d3e2be08966b40949757598e.zip cpython-810a3396d130eda7d3e2be08966b40949757598e.tar.gz cpython-810a3396d130eda7d3e2be08966b40949757598e.tar.bz2 |
Speed up the implementation of quote().
Fix the implementation of quote_plus(). (It wouldn't treat '+' in the
original data right.)
Add urlencode(dict) which is handy to create the data for sending a
POST request with urlopen().
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/urllib.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py index d294c4e..2cb4deb 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -881,22 +881,31 @@ def unquote_plus(s): always_safe = string.letters + string.digits + '_,.-' def quote(s, safe = '/'): safe = always_safe + safe - res = [] - for c in s: - if c in safe: - res.append(c) - else: - res.append('%%%02x' % ord(c)) + res = list(s) + for i in range(len(res)): + c = res[i] + if c not in safe: + res[i] = '%%%02x' % ord(c) return string.joinfields(res, '') def quote_plus(s, safe = '/'): if ' ' in s: # replace ' ' with '+' - s = string.join(string.split(s, ' '), '+') - return quote(s, safe + '+') + l = string.split(s, ' ') + for i in range(len(l)): + l[i] = quote(l[i], safe) + return string.join(l, '+') else: return quote(s, safe) +def urlencode(dict): + l = [] + for k, v in dict.items(): + k = quote_plus(str(k)) + v = quote_plus(str(v)) + l.append(k + '=' + v) + return string.join(l, '&') + # Proxy handling def getproxies(): |