mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
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().
This commit is contained in:
parent
d81fc3cd64
commit
810a3396d1
1 changed files with 17 additions and 8 deletions
|
|
@ -881,22 +881,31 @@ def unquote_plus(s):
|
||||||
always_safe = string.letters + string.digits + '_,.-'
|
always_safe = string.letters + string.digits + '_,.-'
|
||||||
def quote(s, safe = '/'):
|
def quote(s, safe = '/'):
|
||||||
safe = always_safe + safe
|
safe = always_safe + safe
|
||||||
res = []
|
res = list(s)
|
||||||
for c in s:
|
for i in range(len(res)):
|
||||||
if c in safe:
|
c = res[i]
|
||||||
res.append(c)
|
if c not in safe:
|
||||||
else:
|
res[i] = '%%%02x' % ord(c)
|
||||||
res.append('%%%02x' % ord(c))
|
|
||||||
return string.joinfields(res, '')
|
return string.joinfields(res, '')
|
||||||
|
|
||||||
def quote_plus(s, safe = '/'):
|
def quote_plus(s, safe = '/'):
|
||||||
if ' ' in s:
|
if ' ' in s:
|
||||||
# replace ' ' with '+'
|
# replace ' ' with '+'
|
||||||
s = string.join(string.split(s, ' '), '+')
|
l = string.split(s, ' ')
|
||||||
return quote(s, safe + '+')
|
for i in range(len(l)):
|
||||||
|
l[i] = quote(l[i], safe)
|
||||||
|
return string.join(l, '+')
|
||||||
else:
|
else:
|
||||||
return quote(s, safe)
|
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
|
# Proxy handling
|
||||||
def getproxies():
|
def getproxies():
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue