Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3..../urllib
File: request.py
"""An extensible library for opening URLs using a variety of protocols
[0] Fix | Delete
[1] Fix | Delete
The simplest way to use this module is to call the urlopen function,
[2] Fix | Delete
which accepts a string containing a URL or a Request object (described
[3] Fix | Delete
below). It opens the URL and returns the results as file-like
[4] Fix | Delete
object; the returned object has some extra methods described below.
[5] Fix | Delete
[6] Fix | Delete
The OpenerDirector manages a collection of Handler objects that do
[7] Fix | Delete
all the actual work. Each Handler implements a particular protocol or
[8] Fix | Delete
option. The OpenerDirector is a composite object that invokes the
[9] Fix | Delete
Handlers needed to open the requested URL. For example, the
[10] Fix | Delete
HTTPHandler performs HTTP GET and POST requests and deals with
[11] Fix | Delete
non-error returns. The HTTPRedirectHandler automatically deals with
[12] Fix | Delete
HTTP 301, 302, 303 and 307 redirect errors, and the HTTPDigestAuthHandler
[13] Fix | Delete
deals with digest authentication.
[14] Fix | Delete
[15] Fix | Delete
urlopen(url, data=None) -- Basic usage is the same as original
[16] Fix | Delete
urllib. pass the url and optionally data to post to an HTTP URL, and
[17] Fix | Delete
get a file-like object back. One difference is that you can also pass
[18] Fix | Delete
a Request instance instead of URL. Raises a URLError (subclass of
[19] Fix | Delete
OSError); for HTTP errors, raises an HTTPError, which can also be
[20] Fix | Delete
treated as a valid response.
[21] Fix | Delete
[22] Fix | Delete
build_opener -- Function that creates a new OpenerDirector instance.
[23] Fix | Delete
Will install the default handlers. Accepts one or more Handlers as
[24] Fix | Delete
arguments, either instances or Handler classes that it will
[25] Fix | Delete
instantiate. If one of the argument is a subclass of the default
[26] Fix | Delete
handler, the argument will be installed instead of the default.
[27] Fix | Delete
[28] Fix | Delete
install_opener -- Installs a new opener as the default opener.
[29] Fix | Delete
[30] Fix | Delete
objects of interest:
[31] Fix | Delete
[32] Fix | Delete
OpenerDirector -- Sets up the User Agent as the Python-urllib client and manages
[33] Fix | Delete
the Handler classes, while dealing with requests and responses.
[34] Fix | Delete
[35] Fix | Delete
Request -- An object that encapsulates the state of a request. The
[36] Fix | Delete
state can be as simple as the URL. It can also include extra HTTP
[37] Fix | Delete
headers, e.g. a User-Agent.
[38] Fix | Delete
[39] Fix | Delete
BaseHandler --
[40] Fix | Delete
[41] Fix | Delete
internals:
[42] Fix | Delete
BaseHandler and parent
[43] Fix | Delete
_call_chain conventions
[44] Fix | Delete
[45] Fix | Delete
Example usage:
[46] Fix | Delete
[47] Fix | Delete
import urllib.request
[48] Fix | Delete
[49] Fix | Delete
# set up authentication info
[50] Fix | Delete
authinfo = urllib.request.HTTPBasicAuthHandler()
[51] Fix | Delete
authinfo.add_password(realm='PDQ Application',
[52] Fix | Delete
uri='https://mahler:8092/site-updates.py',
[53] Fix | Delete
user='klem',
[54] Fix | Delete
passwd='geheim$parole')
[55] Fix | Delete
[56] Fix | Delete
proxy_support = urllib.request.ProxyHandler({"http" : "http://ahad-haam:3128"})
[57] Fix | Delete
[58] Fix | Delete
# build a new opener that adds authentication and caching FTP handlers
[59] Fix | Delete
opener = urllib.request.build_opener(proxy_support, authinfo,
[60] Fix | Delete
urllib.request.CacheFTPHandler)
[61] Fix | Delete
[62] Fix | Delete
# install it
[63] Fix | Delete
urllib.request.install_opener(opener)
[64] Fix | Delete
[65] Fix | Delete
f = urllib.request.urlopen('http://www.python.org/')
[66] Fix | Delete
"""
[67] Fix | Delete
[68] Fix | Delete
# XXX issues:
[69] Fix | Delete
# If an authentication error handler that tries to perform
[70] Fix | Delete
# authentication for some reason but fails, how should the error be
[71] Fix | Delete
# signalled? The client needs to know the HTTP error code. But if
[72] Fix | Delete
# the handler knows that the problem was, e.g., that it didn't know
[73] Fix | Delete
# that hash algo that requested in the challenge, it would be good to
[74] Fix | Delete
# pass that information along to the client, too.
[75] Fix | Delete
# ftp errors aren't handled cleanly
[76] Fix | Delete
# check digest against correct (i.e. non-apache) implementation
[77] Fix | Delete
[78] Fix | Delete
# Possible extensions:
[79] Fix | Delete
# complex proxies XXX not sure what exactly was meant by this
[80] Fix | Delete
# abstract factory for opener
[81] Fix | Delete
[82] Fix | Delete
import base64
[83] Fix | Delete
import bisect
[84] Fix | Delete
import email
[85] Fix | Delete
import hashlib
[86] Fix | Delete
import http.client
[87] Fix | Delete
import io
[88] Fix | Delete
import os
[89] Fix | Delete
import posixpath
[90] Fix | Delete
import re
[91] Fix | Delete
import socket
[92] Fix | Delete
import string
[93] Fix | Delete
import sys
[94] Fix | Delete
import time
[95] Fix | Delete
import collections
[96] Fix | Delete
import tempfile
[97] Fix | Delete
import contextlib
[98] Fix | Delete
import warnings
[99] Fix | Delete
[100] Fix | Delete
[101] Fix | Delete
from urllib.error import URLError, HTTPError, ContentTooShortError
[102] Fix | Delete
from urllib.parse import (
[103] Fix | Delete
urlparse, urlsplit, urljoin, unwrap, quote, unquote,
[104] Fix | Delete
splittype, splithost, splitport, splituser, splitpasswd,
[105] Fix | Delete
splitattr, splitquery, splitvalue, splittag, to_bytes,
[106] Fix | Delete
unquote_to_bytes, urlunparse)
[107] Fix | Delete
from urllib.response import addinfourl, addclosehook
[108] Fix | Delete
[109] Fix | Delete
# check for SSL
[110] Fix | Delete
try:
[111] Fix | Delete
import ssl
[112] Fix | Delete
except ImportError:
[113] Fix | Delete
_have_ssl = False
[114] Fix | Delete
else:
[115] Fix | Delete
_have_ssl = True
[116] Fix | Delete
[117] Fix | Delete
__all__ = [
[118] Fix | Delete
# Classes
[119] Fix | Delete
'Request', 'OpenerDirector', 'BaseHandler', 'HTTPDefaultErrorHandler',
[120] Fix | Delete
'HTTPRedirectHandler', 'HTTPCookieProcessor', 'ProxyHandler',
[121] Fix | Delete
'HTTPPasswordMgr', 'HTTPPasswordMgrWithDefaultRealm',
[122] Fix | Delete
'HTTPPasswordMgrWithPriorAuth', 'AbstractBasicAuthHandler',
[123] Fix | Delete
'HTTPBasicAuthHandler', 'ProxyBasicAuthHandler', 'AbstractDigestAuthHandler',
[124] Fix | Delete
'HTTPDigestAuthHandler', 'ProxyDigestAuthHandler', 'HTTPHandler',
[125] Fix | Delete
'FileHandler', 'FTPHandler', 'CacheFTPHandler', 'DataHandler',
[126] Fix | Delete
'UnknownHandler', 'HTTPErrorProcessor',
[127] Fix | Delete
# Functions
[128] Fix | Delete
'urlopen', 'install_opener', 'build_opener',
[129] Fix | Delete
'pathname2url', 'url2pathname', 'getproxies',
[130] Fix | Delete
# Legacy interface
[131] Fix | Delete
'urlretrieve', 'urlcleanup', 'URLopener', 'FancyURLopener',
[132] Fix | Delete
]
[133] Fix | Delete
[134] Fix | Delete
# used in User-Agent header sent
[135] Fix | Delete
__version__ = '%d.%d' % sys.version_info[:2]
[136] Fix | Delete
[137] Fix | Delete
_opener = None
[138] Fix | Delete
def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
[139] Fix | Delete
*, cafile=None, capath=None, cadefault=False, context=None):
[140] Fix | Delete
'''Open the URL url, which can be either a string or a Request object.
[141] Fix | Delete
[142] Fix | Delete
*data* must be an object specifying additional data to be sent to
[143] Fix | Delete
the server, or None if no such data is needed. See Request for
[144] Fix | Delete
details.
[145] Fix | Delete
[146] Fix | Delete
urllib.request module uses HTTP/1.1 and includes a "Connection:close"
[147] Fix | Delete
header in its HTTP requests.
[148] Fix | Delete
[149] Fix | Delete
The optional *timeout* parameter specifies a timeout in seconds for
[150] Fix | Delete
blocking operations like the connection attempt (if not specified, the
[151] Fix | Delete
global default timeout setting will be used). This only works for HTTP,
[152] Fix | Delete
HTTPS and FTP connections.
[153] Fix | Delete
[154] Fix | Delete
If *context* is specified, it must be a ssl.SSLContext instance describing
[155] Fix | Delete
the various SSL options. See HTTPSConnection for more details.
[156] Fix | Delete
[157] Fix | Delete
The optional *cafile* and *capath* parameters specify a set of trusted CA
[158] Fix | Delete
certificates for HTTPS requests. cafile should point to a single file
[159] Fix | Delete
containing a bundle of CA certificates, whereas capath should point to a
[160] Fix | Delete
directory of hashed certificate files. More information can be found in
[161] Fix | Delete
ssl.SSLContext.load_verify_locations().
[162] Fix | Delete
[163] Fix | Delete
The *cadefault* parameter is ignored.
[164] Fix | Delete
[165] Fix | Delete
This function always returns an object which can work as a context
[166] Fix | Delete
manager and has methods such as
[167] Fix | Delete
[168] Fix | Delete
* geturl() - return the URL of the resource retrieved, commonly used to
[169] Fix | Delete
determine if a redirect was followed
[170] Fix | Delete
[171] Fix | Delete
* info() - return the meta-information of the page, such as headers, in the
[172] Fix | Delete
form of an email.message_from_string() instance (see Quick Reference to
[173] Fix | Delete
HTTP Headers)
[174] Fix | Delete
[175] Fix | Delete
* getcode() - return the HTTP status code of the response. Raises URLError
[176] Fix | Delete
on errors.
[177] Fix | Delete
[178] Fix | Delete
For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse
[179] Fix | Delete
object slightly modified. In addition to the three new methods above, the
[180] Fix | Delete
msg attribute contains the same information as the reason attribute ---
[181] Fix | Delete
the reason phrase returned by the server --- instead of the response
[182] Fix | Delete
headers as it is specified in the documentation for HTTPResponse.
[183] Fix | Delete
[184] Fix | Delete
For FTP, file, and data URLs and requests explicitly handled by legacy
[185] Fix | Delete
URLopener and FancyURLopener classes, this function returns a
[186] Fix | Delete
urllib.response.addinfourl object.
[187] Fix | Delete
[188] Fix | Delete
Note that None may be returned if no handler handles the request (though
[189] Fix | Delete
the default installed global OpenerDirector uses UnknownHandler to ensure
[190] Fix | Delete
this never happens).
[191] Fix | Delete
[192] Fix | Delete
In addition, if proxy settings are detected (for example, when a *_proxy
[193] Fix | Delete
environment variable like http_proxy is set), ProxyHandler is default
[194] Fix | Delete
installed and makes sure the requests are handled through the proxy.
[195] Fix | Delete
[196] Fix | Delete
'''
[197] Fix | Delete
global _opener
[198] Fix | Delete
if cafile or capath or cadefault:
[199] Fix | Delete
import warnings
[200] Fix | Delete
warnings.warn("cafile, capath and cadefault are deprecated, use a "
[201] Fix | Delete
"custom context instead.", DeprecationWarning, 2)
[202] Fix | Delete
if context is not None:
[203] Fix | Delete
raise ValueError(
[204] Fix | Delete
"You can't pass both context and any of cafile, capath, and "
[205] Fix | Delete
"cadefault"
[206] Fix | Delete
)
[207] Fix | Delete
if not _have_ssl:
[208] Fix | Delete
raise ValueError('SSL support not available')
[209] Fix | Delete
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
[210] Fix | Delete
cafile=cafile,
[211] Fix | Delete
capath=capath)
[212] Fix | Delete
https_handler = HTTPSHandler(context=context)
[213] Fix | Delete
opener = build_opener(https_handler)
[214] Fix | Delete
elif context:
[215] Fix | Delete
https_handler = HTTPSHandler(context=context)
[216] Fix | Delete
opener = build_opener(https_handler)
[217] Fix | Delete
elif _opener is None:
[218] Fix | Delete
_opener = opener = build_opener()
[219] Fix | Delete
else:
[220] Fix | Delete
opener = _opener
[221] Fix | Delete
return opener.open(url, data, timeout)
[222] Fix | Delete
[223] Fix | Delete
def install_opener(opener):
[224] Fix | Delete
global _opener
[225] Fix | Delete
_opener = opener
[226] Fix | Delete
[227] Fix | Delete
_url_tempfiles = []
[228] Fix | Delete
def urlretrieve(url, filename=None, reporthook=None, data=None):
[229] Fix | Delete
"""
[230] Fix | Delete
Retrieve a URL into a temporary location on disk.
[231] Fix | Delete
[232] Fix | Delete
Requires a URL argument. If a filename is passed, it is used as
[233] Fix | Delete
the temporary file location. The reporthook argument should be
[234] Fix | Delete
a callable that accepts a block number, a read size, and the
[235] Fix | Delete
total file size of the URL target. The data argument should be
[236] Fix | Delete
valid URL encoded data.
[237] Fix | Delete
[238] Fix | Delete
If a filename is passed and the URL points to a local resource,
[239] Fix | Delete
the result is a copy from local file to new file.
[240] Fix | Delete
[241] Fix | Delete
Returns a tuple containing the path to the newly created
[242] Fix | Delete
data file as well as the resulting HTTPMessage object.
[243] Fix | Delete
"""
[244] Fix | Delete
url_type, path = splittype(url)
[245] Fix | Delete
[246] Fix | Delete
with contextlib.closing(urlopen(url, data)) as fp:
[247] Fix | Delete
headers = fp.info()
[248] Fix | Delete
[249] Fix | Delete
# Just return the local path and the "headers" for file://
[250] Fix | Delete
# URLs. No sense in performing a copy unless requested.
[251] Fix | Delete
if url_type == "file" and not filename:
[252] Fix | Delete
return os.path.normpath(path), headers
[253] Fix | Delete
[254] Fix | Delete
# Handle temporary file setup.
[255] Fix | Delete
if filename:
[256] Fix | Delete
tfp = open(filename, 'wb')
[257] Fix | Delete
else:
[258] Fix | Delete
tfp = tempfile.NamedTemporaryFile(delete=False)
[259] Fix | Delete
filename = tfp.name
[260] Fix | Delete
_url_tempfiles.append(filename)
[261] Fix | Delete
[262] Fix | Delete
with tfp:
[263] Fix | Delete
result = filename, headers
[264] Fix | Delete
bs = 1024*8
[265] Fix | Delete
size = -1
[266] Fix | Delete
read = 0
[267] Fix | Delete
blocknum = 0
[268] Fix | Delete
if "content-length" in headers:
[269] Fix | Delete
size = int(headers["Content-Length"])
[270] Fix | Delete
[271] Fix | Delete
if reporthook:
[272] Fix | Delete
reporthook(blocknum, bs, size)
[273] Fix | Delete
[274] Fix | Delete
while True:
[275] Fix | Delete
block = fp.read(bs)
[276] Fix | Delete
if not block:
[277] Fix | Delete
break
[278] Fix | Delete
read += len(block)
[279] Fix | Delete
tfp.write(block)
[280] Fix | Delete
blocknum += 1
[281] Fix | Delete
if reporthook:
[282] Fix | Delete
reporthook(blocknum, bs, size)
[283] Fix | Delete
[284] Fix | Delete
if size >= 0 and read < size:
[285] Fix | Delete
raise ContentTooShortError(
[286] Fix | Delete
"retrieval incomplete: got only %i out of %i bytes"
[287] Fix | Delete
% (read, size), result)
[288] Fix | Delete
[289] Fix | Delete
return result
[290] Fix | Delete
[291] Fix | Delete
def urlcleanup():
[292] Fix | Delete
"""Clean up temporary files from urlretrieve calls."""
[293] Fix | Delete
for temp_file in _url_tempfiles:
[294] Fix | Delete
try:
[295] Fix | Delete
os.unlink(temp_file)
[296] Fix | Delete
except OSError:
[297] Fix | Delete
pass
[298] Fix | Delete
[299] Fix | Delete
del _url_tempfiles[:]
[300] Fix | Delete
global _opener
[301] Fix | Delete
if _opener:
[302] Fix | Delete
_opener = None
[303] Fix | Delete
[304] Fix | Delete
# copied from cookielib.py
[305] Fix | Delete
_cut_port_re = re.compile(r":\d+$", re.ASCII)
[306] Fix | Delete
def request_host(request):
[307] Fix | Delete
"""Return request-host, as defined by RFC 2965.
[308] Fix | Delete
[309] Fix | Delete
Variation from RFC: returned value is lowercased, for convenient
[310] Fix | Delete
comparison.
[311] Fix | Delete
[312] Fix | Delete
"""
[313] Fix | Delete
url = request.full_url
[314] Fix | Delete
host = urlparse(url)[1]
[315] Fix | Delete
if host == "":
[316] Fix | Delete
host = request.get_header("Host", "")
[317] Fix | Delete
[318] Fix | Delete
# remove port, if present
[319] Fix | Delete
host = _cut_port_re.sub("", host, 1)
[320] Fix | Delete
return host.lower()
[321] Fix | Delete
[322] Fix | Delete
class Request:
[323] Fix | Delete
[324] Fix | Delete
def __init__(self, url, data=None, headers={},
[325] Fix | Delete
origin_req_host=None, unverifiable=False,
[326] Fix | Delete
method=None):
[327] Fix | Delete
self.full_url = url
[328] Fix | Delete
self.headers = {}
[329] Fix | Delete
self.unredirected_hdrs = {}
[330] Fix | Delete
self._data = None
[331] Fix | Delete
self.data = data
[332] Fix | Delete
self._tunnel_host = None
[333] Fix | Delete
for key, value in headers.items():
[334] Fix | Delete
self.add_header(key, value)
[335] Fix | Delete
if origin_req_host is None:
[336] Fix | Delete
origin_req_host = request_host(self)
[337] Fix | Delete
self.origin_req_host = origin_req_host
[338] Fix | Delete
self.unverifiable = unverifiable
[339] Fix | Delete
if method:
[340] Fix | Delete
self.method = method
[341] Fix | Delete
[342] Fix | Delete
@property
[343] Fix | Delete
def full_url(self):
[344] Fix | Delete
if self.fragment:
[345] Fix | Delete
return '{}#{}'.format(self._full_url, self.fragment)
[346] Fix | Delete
return self._full_url
[347] Fix | Delete
[348] Fix | Delete
@full_url.setter
[349] Fix | Delete
def full_url(self, url):
[350] Fix | Delete
# unwrap('<URL:type://host/path>') --> 'type://host/path'
[351] Fix | Delete
self._full_url = unwrap(url)
[352] Fix | Delete
self._full_url, self.fragment = splittag(self._full_url)
[353] Fix | Delete
self._parse()
[354] Fix | Delete
[355] Fix | Delete
@full_url.deleter
[356] Fix | Delete
def full_url(self):
[357] Fix | Delete
self._full_url = None
[358] Fix | Delete
self.fragment = None
[359] Fix | Delete
self.selector = ''
[360] Fix | Delete
[361] Fix | Delete
@property
[362] Fix | Delete
def data(self):
[363] Fix | Delete
return self._data
[364] Fix | Delete
[365] Fix | Delete
@data.setter
[366] Fix | Delete
def data(self, data):
[367] Fix | Delete
if data != self._data:
[368] Fix | Delete
self._data = data
[369] Fix | Delete
# issue 16464
[370] Fix | Delete
# if we change data we need to remove content-length header
[371] Fix | Delete
# (cause it's most probably calculated for previous value)
[372] Fix | Delete
if self.has_header("Content-length"):
[373] Fix | Delete
self.remove_header("Content-length")
[374] Fix | Delete
[375] Fix | Delete
@data.deleter
[376] Fix | Delete
def data(self):
[377] Fix | Delete
self.data = None
[378] Fix | Delete
[379] Fix | Delete
def _parse(self):
[380] Fix | Delete
self.type, rest = splittype(self._full_url)
[381] Fix | Delete
if self.type is None:
[382] Fix | Delete
raise ValueError("unknown url type: %r" % self.full_url)
[383] Fix | Delete
self.host, self.selector = splithost(rest)
[384] Fix | Delete
if self.host:
[385] Fix | Delete
self.host = unquote(self.host)
[386] Fix | Delete
[387] Fix | Delete
def get_method(self):
[388] Fix | Delete
"""Return a string indicating the HTTP request method."""
[389] Fix | Delete
default_method = "POST" if self.data is not None else "GET"
[390] Fix | Delete
return getattr(self, 'method', default_method)
[391] Fix | Delete
[392] Fix | Delete
def get_full_url(self):
[393] Fix | Delete
return self.full_url
[394] Fix | Delete
[395] Fix | Delete
def set_proxy(self, host, type):
[396] Fix | Delete
if self.type == 'https' and not self._tunnel_host:
[397] Fix | Delete
self._tunnel_host = self.host
[398] Fix | Delete
else:
[399] Fix | Delete
self.type= type
[400] Fix | Delete
self.selector = self.full_url
[401] Fix | Delete
self.host = host
[402] Fix | Delete
[403] Fix | Delete
def has_proxy(self):
[404] Fix | Delete
return self.selector == self.full_url
[405] Fix | Delete
[406] Fix | Delete
def add_header(self, key, val):
[407] Fix | Delete
# useful for something like authentication
[408] Fix | Delete
self.headers[key.capitalize()] = val
[409] Fix | Delete
[410] Fix | Delete
def add_unredirected_header(self, key, val):
[411] Fix | Delete
# will not be added to a redirected request
[412] Fix | Delete
self.unredirected_hdrs[key.capitalize()] = val
[413] Fix | Delete
[414] Fix | Delete
def has_header(self, header_name):
[415] Fix | Delete
return (header_name in self.headers or
[416] Fix | Delete
header_name in self.unredirected_hdrs)
[417] Fix | Delete
[418] Fix | Delete
def get_header(self, header_name, default=None):
[419] Fix | Delete
return self.headers.get(
[420] Fix | Delete
header_name,
[421] Fix | Delete
self.unredirected_hdrs.get(header_name, default))
[422] Fix | Delete
[423] Fix | Delete
def remove_header(self, header_name):
[424] Fix | Delete
self.headers.pop(header_name, None)
[425] Fix | Delete
self.unredirected_hdrs.pop(header_name, None)
[426] Fix | Delete
[427] Fix | Delete
def header_items(self):
[428] Fix | Delete
hdrs = self.unredirected_hdrs.copy()
[429] Fix | Delete
hdrs.update(self.headers)
[430] Fix | Delete
return list(hdrs.items())
[431] Fix | Delete
[432] Fix | Delete
class OpenerDirector:
[433] Fix | Delete
def __init__(self):
[434] Fix | Delete
client_version = "Python-urllib/%s" % __version__
[435] Fix | Delete
self.addheaders = [('User-agent', client_version)]
[436] Fix | Delete
# self.handlers is retained only for backward compatibility
[437] Fix | Delete
self.handlers = []
[438] Fix | Delete
# manage the individual handlers
[439] Fix | Delete
self.handle_open = {}
[440] Fix | Delete
self.handle_error = {}
[441] Fix | Delete
self.process_response = {}
[442] Fix | Delete
self.process_request = {}
[443] Fix | Delete
[444] Fix | Delete
def add_handler(self, handler):
[445] Fix | Delete
if not hasattr(handler, "add_parent"):
[446] Fix | Delete
raise TypeError("expected BaseHandler instance, got %r" %
[447] Fix | Delete
type(handler))
[448] Fix | Delete
[449] Fix | Delete
added = False
[450] Fix | Delete
for meth in dir(handler):
[451] Fix | Delete
if meth in ["redirect_request", "do_open", "proxy_open"]:
[452] Fix | Delete
# oops, coincidental match
[453] Fix | Delete
continue
[454] Fix | Delete
[455] Fix | Delete
i = meth.find("_")
[456] Fix | Delete
protocol = meth[:i]
[457] Fix | Delete
condition = meth[i+1:]
[458] Fix | Delete
[459] Fix | Delete
if condition.startswith("error"):
[460] Fix | Delete
j = condition.find("_") + i + 1
[461] Fix | Delete
kind = meth[j+1:]
[462] Fix | Delete
try:
[463] Fix | Delete
kind = int(kind)
[464] Fix | Delete
except ValueError:
[465] Fix | Delete
pass
[466] Fix | Delete
lookup = self.handle_error.get(protocol, {})
[467] Fix | Delete
self.handle_error[protocol] = lookup
[468] Fix | Delete
elif condition == "open":
[469] Fix | Delete
kind = protocol
[470] Fix | Delete
lookup = self.handle_open
[471] Fix | Delete
elif condition == "response":
[472] Fix | Delete
kind = protocol
[473] Fix | Delete
lookup = self.process_response
[474] Fix | Delete
elif condition == "request":
[475] Fix | Delete
kind = protocol
[476] Fix | Delete
lookup = self.process_request
[477] Fix | Delete
else:
[478] Fix | Delete
continue
[479] Fix | Delete
[480] Fix | Delete
handlers = lookup.setdefault(kind, [])
[481] Fix | Delete
if handlers:
[482] Fix | Delete
bisect.insort(handlers, handler)
[483] Fix | Delete
else:
[484] Fix | Delete
handlers.append(handler)
[485] Fix | Delete
added = True
[486] Fix | Delete
[487] Fix | Delete
if added:
[488] Fix | Delete
bisect.insort(self.handlers, handler)
[489] Fix | Delete
handler.add_parent(self)
[490] Fix | Delete
[491] Fix | Delete
def close(self):
[492] Fix | Delete
# Only exists for backwards compatibility.
[493] Fix | Delete
pass
[494] Fix | Delete
[495] Fix | Delete
def _call_chain(self, chain, kind, meth_name, *args):
[496] Fix | Delete
# Handlers raise an exception if no one else should try to handle
[497] Fix | Delete
# the request, or return None if they can't but another handler
[498] Fix | Delete
# could. Otherwise, they return the response.
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function