本文整理汇总了Python中urllib.quote函数的典型用法代码示例。如果您正苦于以下问题:Python quote函数的具体用法?Python quote怎么用?Python quote使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了quote函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: application
def application(req):
#doGzip = 0
#try:
# if string.find(os.environ["HTTP_ACCEPT_ENCODING"], "gzip") != -1:
# doGzip = 1
#except:
# pass
treeid = req.params.get("treeid")
module = req.params.get("module")
branch = req.params.get("branch")
mindate = req.params.get("mindate")
maxdate = req.params.get("maxdate")
xml_nofiles = req.params.get("xml_nofiles")
if not treeid or not module or not branch or not mindate or not maxdate:
raise exc.HTTPBadRequest("ERROR")
url = bonsai + "?" + "branchtype=match&sortby=Date&date=explicit&cvsroot=%2Fcvsroot&xml=1"
url += "&treeid=%s&module=%s&branch=%s&mindate=%s&maxdate=%s" % (quote(treeid), quote(module), quote(branch), quote(mindate), quote(maxdate))
if xml_nofiles:
url += "&xml_nofiles=1"
urlstream = urllib.urlopen(url)
resp = Response(content_type='text/xml')
for s in urlstream:
resp.write(s)
urlstream.close()
return resp
示例2: tag_feed
def tag_feed(request, tag, full_text = False):
# horrible special-case for the tag feed
server = "http://%s"%(request.META['SERVER_NAME'] or 'localhost')
feedclass = feed_engine( 'atom' )
feed = feedclass(
title = 'things tagged %s on jerakeen.org'%tag,
link = "%s/tags/%s/"%(server, urllib.quote(tag.encode('utf-8'))),
description = "",
author_name = "Tom Insam",
author_email = "[email protected]",
author_link = "http://jerakeen.org/",
feed_url = "%s/tags/%s/feed/"%(server,urllib.quote(tag.encode('utf-8'))),
);
tags = filter( lambda x: len(x), tag.split('+') )
page_list = Page.pages_tagged(tags)[:15]
for p in page_list:
if full_text:
data = p.body
else:
data = p.excerpt or p.body
image = '<img src="%s" style="float:left" width="75" height="75">'%p.thumb_url()
feed.add_item(
p.title,
p.really_absolute_url(),
image + pagefilter( data ),
pubdate = p.date )
return HttpResponse(
feed.writeString('utf-8'), mimetype = feed.mime_type )
示例3: handle_wowwiki
def handle_wowwiki(bot, ievent):
""" wikipedia <what> .. search wikipedia for <what> """
if not ievent.rest:
ievent.missing('<what>')
return
what = ""
lang = 'en'
for i in ievent.rest.split():
first = i[0].upper()
rest = i[1:]
if i.startswith('-'):
if len(i) != 3:
ievent.reply('invalid option')
return
lang = i[1:]
continue
what += "%s%s " % (first, rest)
what = what.strip().replace(' ', '_')
url = 'http://wowwiki.com/wiki/Special:Export/%s' % quote(what.encode('utf-8'))
url2 = 'http://wowwiki.com/wiki/%s' % quote(what.encode('utf-8'))
txt = getwikidata(url, ievent)
if not txt:
return
if '#REDIRECT' in txt or '#redirect' in txt:
redir = ' '.join(txt.split()[1:])
url = 'http://wowwiki.com/wiki/Special:Export/%s' % quote(redir.encode('utf-8'))
url2 = 'http://wowwiki.com/wiki/%s' % quote(redir.encode('utf-8'))
txt = getwikidata(url, ievent)
if not txt:
return
res = ['%s ===> ' % url2, ]
res += splittxt(striphtml(txt).strip())
ievent.reply(res)
示例4: _encode_multipart
def _encode_multipart(vars, content_type):
"""Encode a multipart request body into a string"""
boundary_match = re.search(r'boundary=([^ ]+)', content_type, re.I)
if not boundary_match:
raise ValueError('Content-type: %r does not contain boundary' % content_type)
boundary = boundary_match.group(1).strip('"')
lines = []
for name, value in vars.iteritems():
lines.append('--%s' % boundary)
## FIXME: encode the name like this?
assert name is not None, 'Value associated with no name: %r' % value
disp = 'Content-Disposition: form-data; name="%s"' % urllib.quote(name)
if getattr(value, 'filename', None):
disp += '; filename="%s"' % urllib.quote(value.filename)
lines.append(disp)
## FIXME: should handle value.disposition_options
if getattr(value, 'type', None):
ct = 'Content-type: %s' % value.type
if value.type_options:
ct += ''.join(['; %s="%s"' % (ct_name, urllib.quote(ct_value))
for ct_name, ct_value in sorted(value.type_options.items())])
lines.append(ct)
lines.append('')
if hasattr(value, 'value'):
lines.append(value.value)
else:
lines.append(value)
lines.append('--%s--' % boundary)
return '\r\n'.join(lines)
示例5: urlencode
def urlencode(self, safe=None):
"""
Returns an encoded string of all query string arguments.
:arg safe: Used to specify characters which do not require quoting, for
example::
>>> q = QueryDict('', mutable=True)
>>> q['next'] = '/a&b/'
>>> q.urlencode()
'next=%2Fa%26b%2F'
>>> q.urlencode(safe='/')
'next=/a%26b/'
"""
output = []
if safe:
encode = lambda k, v: '%s=%s' % ((quote(k, safe), quote(v, safe)))
else:
encode = lambda k, v: urlencode({k: v})
for k, list_ in self.lists():
k = smart_str(k, self.encoding)
output.extend([encode(k, smart_str(v, self.encoding))
for v in list_])
return '&'.join(output)
示例6: test_default_quoting
def test_default_quoting(self):
# Make sure all characters that should be quoted are by default sans
# space (separate test for that).
should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
should_quote.append('<>#%"{}|\^[]`')
should_quote.append(chr(127)) # For 0x7F
should_quote = "".join(should_quote)
for char in should_quote:
result = urllib.quote(char)
self.assertEqual(
hexescape(char),
result,
"using quote(): %s should be escaped to %s, not %s" % (char, hexescape(char), result),
)
result = urllib.quote_plus(char)
self.assertEqual(
hexescape(char),
result,
"using quote_plus(): " "%s should be escapes to %s, not %s" % (char, hexescape(char), result),
)
del should_quote
partial_quote = "ab[]cd"
expected = "ab%5B%5Dcd"
result = urllib.quote(partial_quote)
self.assertEqual(expected, result, "using quote(): %s != %s" % (expected, result))
result = urllib.quote_plus(partial_quote)
self.assertEqual(expected, result, "using quote_plus(): %s != %s" % (expected, result))
self.assertRaises(TypeError, urllib.quote, None)
示例7: _gen_signed_data
def _gen_signed_data(self, base_url, method="GET", sign_method='HMAC-SHA1', **params):
args = {'oauth_consumer_key': self.consumer_key,
'oauth_timestamp': self.__timestamp(),
'oauth_nonce': self.__nonce(),
'oauth_version': '1.0'}
args.update(params)
if sign_method == 'HMAC-SHA1':
args['oauth_signature_method'] = 'HMAC-SHA1'
key = self.consumer_secret + "&"
if self.token is not None:
args['oauth_token'] = self.token.oauth_token
key += urllib.quote(self.token.oauth_token_secret, '')
#would use urlencode, but it doesn't sort arguments
#pargs = [sorted('%s=%s' % (k,v) for k,v in args.values())]
message = '&'.join(
urllib.quote(i, '') for i in [method.upper(), base_url,
urllib.urlencode(sorted(args.iteritems()))])
args['oauth_signature'] = hmac.new(key, message, hashlib.sha1
).digest().encode('base64')[:-1]
# Add other sign_methods here
else:
raise self.UnknownSignatureException("Unknown signature method %s" % sign_method)
return args
示例8: replace_or_add_query
def replace_or_add_query(url, query, exclusions=None):
"""
Adds field/value pair to the provided url as a query string if the
key isn't already in the url, or replaces it otherwise.
Appends the proper pair separator (?&) based on the input url
Inputs:
:url: URL that query string should be appended to
:query: Query string(s) to add to :url:
:exclusions: List of keys that should not be copied; common keys
include 'vs' and 'z'
Outputs:
:url: Input url with query string appended
"""
if not exclusions:
exclusions = []
if len(query) > 1 and query[0] in ['?', '&']:
query = query[1:]
query = query.encode('utf-8')
url = url.encode('utf-8')
url = urlparse.urlparse(url)
old_query = urlparse.parse_qsl(url.query, keep_blank_values=True)
old_keys = [q[0] for q in old_query]
# make a lower-case copy of old_keys so we can do some comparisons
insensitive_keys = map(str.lower, old_keys)
new_query = urlparse.parse_qsl(query, keep_blank_values=True)
# For each source code that we are going to add
for new_index in range(len(new_query)):
# Make sure we are not adding a source code that should be excluded
if new_query[new_index][0] not in exclusions:
try:
# Case-insensitively determine if the new source code
# is already applied
old_index = insensitive_keys.index(
new_query[new_index][0].lower())
except ValueError:
# The current source code is not applied; apply it
old_query.append(new_query[new_index])
else:
# The current source code is already applied; replace its
# value, keeping the case of the old parameter
old_query[old_index] = (old_query[old_index][0],
new_query[new_index][1])
# parse_qsl unencodes the query that you pass it; Re-encode the query
# parameters when reconstructing the string.
old_query = '&'.join(['='.join([urllib.quote(k, safe=','),
urllib.quote(v, safe=',')])
for k, v in old_query])
url = url._replace(query=old_query)
url = urlparse.urlunparse(url)
else:
parts = url.split('#')
parts[0] += query
url = '#'.join(parts)
return url
示例9: handle_upload
def handle_upload(self):
results = []
blob_keys = []
for name, fieldStorage in self.request.POST.items():
if type(fieldStorage) is unicode:
continue
result = {}
result['name'] = re.sub(r'^.*\\','',fieldStorage.filename)
result['type'] = fieldStorage.type
result['size'] = self.get_file_size(fieldStorage.file)
if self.validate(result):
blob_key = str(self.write_blob(fieldStorage.value, result))
blob_keys.append(blob_key)
result['deleteType'] = 'DELETE'
result['deleteUrl'] = self.request.host_url +\
'/?key=' + urllib.quote(blob_key, '')
if (IMAGE_TYPES.match(result['type'])):
try:
result['url'] = images.get_serving_url(
blob_key,
secure_url=self.request.host_url.startswith(
'https'
)
)
result['thumbnailUrl'] = result['url'] +\
THUMBNAIL_MODIFICATOR
except: # Could not get an image serving url
pass
if not 'url' in result:
result['url'] = self.request.host_url +\
'/' + blob_key + '/' + urllib.quote(
result['name'].encode('utf-8'), '')
results.append(result)
deferred.defer(cleanup,blob_keys,_countdown=EXPIRATION_TIME)
return results
示例10: execute_request
def execute_request(self, method, *stuff) :
enc_method = urllib.quote(method)
url = self.host + self.endpoint + enc_method + ".json"
if len(stuff) :
enc_stuff = []
for thing in stuff :
if not thing :
continue
enc_stuff.append(urllib.quote(thing))
if len(enc_stuff) :
url = "%s/%s" % (url, "/".join(enc_stuff))
data = None
headers = {
'api_key' : self.api_key,
}
try :
req = urllib2.Request(url, data, headers)
res = urllib2.urlopen(req)
except Exception, e :
raise e
示例11: Configure
def Configure(self, prefix="XBMC-Event", xbmcip="192.168.1.1", xbmchttpport=8080, zone="224.0.0.2", port=8278, selfXbmceventbroadcast=False, payDelim="<b></b>"):
panel = eg.ConfigPanel(self)
editCtrl = panel.TextCtrl(prefix)
xbmcipCtrl = panel.TextCtrl(xbmcip)
xbmchttpportCtrl = panel.SpinIntCtrl(xbmchttpport, min=1, max=65535)
zoneCtrl = panel.TextCtrl(zone)
portCtrl = panel.SpinIntCtrl(port, min=1, max=65535)
selfXbmceventbroadcastCtrl=panel.CheckBox(selfXbmceventbroadcast)
payDelimCtrl = panel.TextCtrl(payDelim)
panel.AddLine(self.text.eventPrefix, editCtrl)
panel.AddLine(self.text.xbmcip, xbmcipCtrl)
panel.AddLine(self.text.xbmchttpport, xbmchttpportCtrl)
panel.AddLine(self.text.zone, zoneCtrl)
panel.AddLine(self.text.port, portCtrl)
panel.AddLine(self.text.selfXbmceventbroadcast,selfXbmceventbroadcastCtrl)
panel.AddLine("Payload Delimiter", payDelimCtrl)
while panel.Affirmed():
panel.SetResult(editCtrl.GetValue(),xbmcipCtrl.GetValue(),int(xbmchttpportCtrl.GetValue()),zoneCtrl.GetValue(),int(portCtrl.GetValue()),selfXbmceventbroadcastCtrl.GetValue(), payDelimCtrl.GetValue() )
v_header = urllib.quote("This is the Header")
v_message = urllib.quote("This is the Message")
host_xbmc = xbmcipCtrl.GetValue()
port_xbmc = int(xbmchttpportCtrl.GetValue())
udp_xbmc = int(portCtrl.GetValue())
url_xbmc = "http://" + str(host_xbmc) + ":" + str(port_xbmc) + "/xbmcCmds/xbmcHttp?command=SetBroadcast¶meter=2;" + str(udp_xbmc) + "(Notification(" + v_header + "," + v_message + "))"
print "str(url_xbmc)"
try:
urllib.urlopen(url_xbmc)
except IOError:
print 'Connection error'
示例12: get_authenticated_user
def get_authenticated_user(self, server_ticket):
"""
Requests the user's information from the CAS server using the given
*server_ticket* and calls ``self._on_auth`` with the resulting user
dict.
"""
cas_version = self.settings.get('cas_version', 2)
cas_server = self.settings.get('cas_server')
ca_certs = self.settings.get('cas_ca_certs', None)
if not cas_server.endswith('/'):
cas_server += '/'
service_url = "%sauth" % self.base_url
#validate the ST
validate_suffix = 'proxyValidate'
if cas_version == 1:
validate_suffix = 'validate'
validate_url = (
cas_server +
validate_suffix +
'?service=' +
quote(service_url) +
'&ticket=' +
quote(server_ticket)
)
logging.debug("Fetching CAS URL: %s" % validate_url)
validate_cert = False
if ca_certs:
validate_cert = True
http_client = tornado.httpclient.AsyncHTTPClient()
http_client.fetch(
validate_url, validate_cert=validate_cert, callback=self._on_auth)
示例13: _get_all
def _get_all(self, element_map, initial_query_string='',
headers=None, **params):
l = []
for k,v in params.items():
k = k.replace('_', '-')
if k == 'maxkeys':
k = 'max-keys'
if isinstance(v, unicode):
v = v.encode('utf-8')
if v is not None and v != '':
l.append('%s=%s' % (urllib.quote(k), urllib.quote(str(v))))
if len(l):
s = initial_query_string + '&' + '&'.join(l)
else:
s = initial_query_string
response = self.connection.make_request('GET', self.name,
headers=headers, query_args=s)
body = response.read()
boto.log.debug(body)
if response.status == 200:
rs = ResultSet(element_map)
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise self.connection.provider.storage_response_error(
response.status, response.reason, body)
示例14: find_books
def find_books(self, search_text):
if _NEW_TOOLBAR_SUPPORT:
self.enable_button(False)
else:
self._books_toolbar.enable_button(False)
self.clear_downloaded_bytes()
textbuffer = self.textview.get_buffer()
textbuffer.set_text(_('Performing lookup, please wait') + '...')
self.book_selected = False
self.ls.clear()
search_tuple = search_text.lower().split()
if len(search_tuple) == 0:
self._alert(_('Error'), _('You must enter at least one search word.'))
if _NEW_TOOLBAR_SUPPORT:
self.search_entry.grab_focus()
else:
self._books_toolbar.search_entry.grab_focus()
return
FL = urllib.quote('fl[]')
SORT = urllib.quote('sort[]')
self.search_url = 'http://www.archive.org/advancedsearch.php?q=' + \
urllib.quote('(title:(' + search_text.lower() + ') OR creator:(' + search_text.lower() +')) AND format:(DJVU)')
self.search_url += '&' + FL + '=creator&' + FL + '=description&' + FL + '=format&' + FL + '=identifier&' \
+ FL + '=language'
self.search_url += '&' + FL + '=publisher&' + FL + '=subject&' + FL + '=title&' + FL + '=volume'
self.search_url += '&' + SORT + '=title&' + SORT + '&' + SORT + '=&rows=500&save=yes&fmt=csv&xmlsearch=Search'
GObject.idle_add(self.download_csv, self.search_url)
示例15: get_current_url
def get_current_url(environ, root_only=False, strip_querystring=False,
host_only=False):
"""A handy helper function that recreates the full URL for the current
request or parts of it. Here an example:
>>> from werkzeug import create_environ
>>> env = create_environ("/?param=foo", "http://localhost/script")
>>> get_current_url(env)
'http://localhost/script/?param=foo'
>>> get_current_url(env, root_only=True)
'http://localhost/script/'
>>> get_current_url(env, host_only=True)
'http://localhost/'
>>> get_current_url(env, strip_querystring=True)
'http://localhost/script/'
:param environ: the WSGI environment to get the current URL from.
:param root_only: set `True` if you only want the root URL.
:param strip_querystring: set to `True` if you don't want the querystring.
:param host_only: set to `True` if the host URL should be returned.
"""
tmp = [environ['wsgi.url_scheme'], '://', get_host(environ)]
cat = tmp.append
if host_only:
return ''.join(tmp) + '/'
cat(urllib.quote(environ.get('SCRIPT_NAME', '').rstrip('/')))
if root_only:
cat('/')
else:
cat(urllib.quote('/' + environ.get('PATH_INFO', '').lstrip('/')))
if not strip_querystring:
qs = environ.get('QUERY_STRING')
if qs:
cat('?' + qs)
return ''.join(tmp)