def urlparts(self):
""" The :attr:`url` string as an :class:`urlparse.SplitResult` tuple.
The tuple contains (scheme, host, path, query_string and fragment),
but the fragment is always empty because it is not visible to the
server. """
env = self.environ
http = env.get('HTTP_X_FORWARDED_PROTO') or env.get('wsgi.url_scheme', 'http')
host = env.get('HTTP_X_FORWARDED_HOST') or env.get('HTTP_HOST')
if not host:
# HTTP 1.1 requires a Host-header. This is for HTTP/1.0 clients.
host = env.get('SERVER_NAME', '127.0.0.1')
port = env.get('SERVER_PORT')
if port and port != ('80' if http == 'http' else '443'):
host += ':' + port
path = urlquote(self.fullpath)
return UrlSplitResult(http, host, path, env.get('QUERY_STRING'), '')
python类quote()的实例源码
def urlparts(self):
""" The :attr:`url` string as an :class:`urlparse.SplitResult` tuple.
The tuple contains (scheme, host, path, query_string and fragment),
but the fragment is always empty because it is not visible to the
server. """
env = self.environ
http = env.get('HTTP_X_FORWARDED_PROTO') \
or env.get('wsgi.url_scheme', 'http')
host = env.get('HTTP_X_FORWARDED_HOST') or env.get('HTTP_HOST')
if not host:
# HTTP 1.1 requires a Host-header. This is for HTTP/1.0 clients.
host = env.get('SERVER_NAME', '127.0.0.1')
port = env.get('SERVER_PORT')
if port and port != ('80' if http == 'http' else '443'):
host += ':' + port
path = urlquote(self.fullpath)
return UrlSplitResult(http, host, path, env.get('QUERY_STRING'), '')
def _get_creds(self):
"""
Assumes into the target account and obtains Access Key, Secret Key, and Token
:return: URL-encoded dictionary containing Access Key, Secret Key, and Token
"""
client, credentials = boto3_cached_conn(
'iam', account_number=self.account_number, assume_role=self.role_name, return_credentials=True)
creds = json.dumps(dict(
sessionId=credentials['AccessKeyId'],
sessionKey=credentials['SecretAccessKey'],
sessionToken=credentials['SessionToken']
))
creds = urllib.quote(creds, safe='')
return creds
def makeInternalLink(title, label):
colon = title.find(':')
if colon > 0 and title[:colon] not in options.acceptedNamespaces:
return ''
if colon == 0:
# drop also :File:
colon2 = title.find(':', colon + 1)
if colon2 > 1 and title[colon + 1:colon2] not in options.acceptedNamespaces:
return ''
if options.keepLinks:
return '<a href="%s">%s</a>' % (quote(title.encode('utf-8')), label)
else:
return label
# ----------------------------------------------------------------------
# External links
# from: https://doc.wikimedia.org/mediawiki-core/master/php/DefaultSettings_8php_source.html
def SearchEngine(search):
searchUrl = 'https://www.underanime.net/?s='
search = urllib.quote(search)
page = GetUrl(searchUrl + search)
animeLinks = page.xpath('//*/div/div/div/div[@class="base_inner h255 loading"]/a/@href')
animeNames = page.xpath('//*/div/div/div/div[@class="base_inner h255 loading"]/a/@title')
#check links, todo better
linkNum = len(animeLinks)
if linkNum != len(animeNames):
print '[!] Error, some links missing!'
return False
#create anime list
animeList = []
for n in range(0, linkNum):
anime = Anime(animeNames[n].encode(sys.stdout.encoding, errors='replace'), animeLinks[n], n)
animeList.append(anime)
return animeList
def query(kw):
for i in range(1, 10):
encode_kw = urllib.quote(kw)
print i
url = 'https://m.anjuke.com/ajax/autocomplete/?city_id=13&kw=%s&from=1&callback=jsonp%d' % (encode_kw, i)
s = requests.Session()
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}
js = s.get(url, headers=headers)
print js.status_code
# print js.text
try:
result = re.findall('jsonp7\((.*?)\);', js.text)[0]
dic = json.loads(result)
print '*' * 20
print dic['data']['match'][0]['comm_id']
except Exception, e:
print e
# ??????????
def query_rrset(self, oname, rrtype=None, bailiwick=None, before=None, after=None):
if bailiwick:
if not rrtype:
rrtype = 'ANY'
path = 'rrset/name/%s/%s/%s' % (quote(oname), rrtype, quote(bailiwick))
elif rrtype:
path = 'rrset/name/%s/%s' % (quote(oname), rrtype)
else:
path = 'rrset/name/%s' % quote(oname)
return self._query(path, before, after)
def query_rdata_name(self, rdata_name, rrtype=None, before=None, after=None):
if rrtype:
path = 'rdata/name/%s/%s' % (quote(rdata_name), rrtype)
else:
path = 'rdata/name/%s' % quote(rdata_name)
return self._query(path, before, after)
def quote(path):
return urllib.quote(path, safe='')
def pathname2url(p):
"""OS-specific conversion from a file system path to a relative URL
of the 'file' scheme; not recommended for general use."""
# e.g.
# C:\foo\bar\spam.foo
# becomes
# ///C|/foo/bar/spam.foo
import urllib
if not ':' in p:
# No drive specifier, just convert slashes and quote the name
if p[:2] == '\\\\':
# path is something like \\host\path\on\remote\host
# convert this to ////host/path/on/remote/host
# (notice doubling of slashes at the start of the path)
p = '\\\\' + p
components = p.split('\\')
return urllib.quote('/'.join(components))
comp = p.split(':')
if len(comp) != 2 or len(comp[0]) > 1:
error = 'Bad path: ' + p
raise IOError, error
drive = urllib.quote(comp[0].upper())
components = comp[1].split('\\')
path = '///' + drive + ':'
for comp in components:
if comp:
path = path + '/' + urllib.quote(comp)
return path
def __init__(self, path, allowance):
if path == '' and not allowance:
# an empty value means allow all
allowance = True
self.path = urllib.quote(path)
self.allowance = allowance
def encode_rfc2231(s, charset=None, language=None):
"""Encode string according to RFC 2231.
If neither charset nor language is given, then s is returned as-is. If
charset is given but not language, the string is encoded using the empty
string for language.
"""
import urllib
s = urllib.quote(s, safe='')
if charset is None and language is None:
return s
if language is None:
language = ''
return "%s'%s'%s" % (charset, language, s)
def request_uri(environ, include_query=1):
"""Return the full request URI, optionally including the query string"""
url = application_uri(environ)
from urllib import quote
path_info = quote(environ.get('PATH_INFO',''),safe='/;=,')
if not environ.get('SCRIPT_NAME'):
url += path_info[1:]
else:
url += path_info
if include_query and environ.get('QUERY_STRING'):
url += '?' + environ['QUERY_STRING']
return url
def escape_path(path):
"""Escape any invalid characters in HTTP URL, and uppercase all escapes."""
# There's no knowing what character encoding was used to create URLs
# containing %-escapes, but since we have to pick one to escape invalid
# path characters, we pick UTF-8, as recommended in the HTML 4.0
# specification:
# http://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.2.1
# And here, kind of: draft-fielding-uri-rfc2396bis-03
# (And in draft IRI specification: draft-duerst-iri-05)
# (And here, for new URI schemes: RFC 2718)
if isinstance(path, unicode):
path = path.encode("utf-8")
path = urllib.quote(path, HTTP_PATH_SAFE)
path = ESCAPED_CHAR_RE.sub(uppercase_escaped_char, path)
return path
def _pncomp2url(component):
component = urllib.quote(component[:31], safe='') # We want to quote slashes
return component
def watchlist_modify(self, id, watchlist):
"""
updates a watchlist
"""
url = "%s/api/v1/watchlist/%s" % (self.server, id)
watchlist['search_query'] = urllib.quote(watchlist['search_query'])
# ensure that it starts with the proper url parameters
if not watchlist['search_query'].startswith("cb.urlver=1&q="):
watchlist['search_query'] = "cb.urlver=1&q=" + watchlist['search_query']
r = self.cbapi_put(url, data=json.dumps(watchlist))
r.raise_for_status()
return r.json()
def send(self, *a, **kw):
a[0].url = a[0].url.replace(urllib.quote("<"), "<")
a[0].url = a[0].url.replace(urllib.quote(" "), " ")
a[0].url = a[0].url.replace(urllib.quote(">"), ">")
return requests.Session.send(self, *a, **kw)
def make_make_email_data(to, cc=None, bcc=None, subject=None, body=None):
"""\
Creates either a simple "mailto:" URL or complete e-mail message with
(blind) carbon copies and a subject and a body.
:param str|iterable to: The email address (recipient). Multiple
values are allowed.
:param str|iterable|None cc: The carbon copy recipient. Multiple
values are allowed.
:param str|iterable|None bcc: The blind carbon copy recipient.
Multiple values are allowed.
:param str|None subject: The subject.
:param str|None body: The message body.
"""
def multi(val):
if not val:
return ()
if isinstance(val, str_type):
return (val,)
return tuple(val)
delim = '?'
data = ['mailto:']
if not to:
raise ValueError('"to" must not be empty or None')
data.append(','.join(multi(to)))
for key, val in (('cc', cc), ('bcc', bcc)):
vals = multi(val)
if vals:
data.append('{0}{1}={2}'.format(delim, key, ','.join(vals)))
delim = '&'
for key, val in (('subject', subject), ('body', body)):
if val is not None:
data.append('{0}{1}={2}'.format(delim, key, quote(val.encode('utf-8'))))
delim = '&'
return ''.join(data)
def as_svg_data_uri(matrix, version, scale=1, border=None, color='#000',
background=None, xmldecl=False, svgns=True, title=None,
desc=None, svgid=None, svgclass='segno',
lineclass='qrline', omitsize=False, unit='',
encoding='utf-8', svgversion=None, nl=False,
encode_minimal=False, omit_charset=False):
"""\
Converts the matrix to a SVG data URI.
The XML declaration is omitted by default (set ``xmldecl`` to ``True``
to enable it), further the newline is omitted by default (set ``nl`` to
``True`` to enable it).
Aside from the missing ``out`` parameter and the different ``xmldecl``
and ``nl`` default values and the additional parameter ``encode_minimal``
and ``omit_charset`` this function uses the same parameters as the
usual SVG serializer.
:param bool encode_minimal: Indicates if the resulting data URI should
use minimal percent encoding (disabled by default).
:param bool omit_charset: Indicates if the ``;charset=...`` should be omitted
(disabled by default)
:rtype: str
"""
encode = partial(quote, safe=b"") if not encode_minimal else partial(quote, safe=b" :/='")
buff = io.BytesIO()
write_svg(matrix, version, buff, scale=scale, color=color, background=background,
border=border, xmldecl=xmldecl, svgns=svgns, title=title,
desc=desc, svgclass=svgclass, lineclass=lineclass,
omitsize=omitsize, encoding=encoding, svgid=svgid, unit=unit,
svgversion=svgversion, nl=nl)
return 'data:image/svg+xml{0},{1}' \
.format(';charset=' + encoding if not omit_charset else '',
# Replace " quotes with ' and URL encode the result
# See also https://codepen.io/tigt/post/optimizing-svgs-in-data-uris
encode(_replace_quotes(buff.getvalue())))
def __handle_escape(self, key):
while True:
start_js = self.js
offset = self.js.find(key) + len(key)
if self.js[offset] == '(' and self.js[offset + 2] == ')':
c = self.js[offset + 1]
self.js = self.js.replace('%s(%s)' % (key, c), urllib.quote(c))
if start_js == self.js:
break