Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def request(self, method, url, body=None, headers={}):
self.method = method
self.selector = url
self.req_headers += list(iteritems(headers))
self.req_headers.sort()
if body:
self.data = body
if self.raise_on_endheaders:
import socket
raise socket.error()
def check_cache(self):
# first check for old ones
t = time.time()
if self.soonest <= t:
for k, v in iteritems(self.timeout):
if v < t:
self.cache[k].close()
del self.cache[k]
del self.timeout[k]
self.soonest = min(self.timeout.values())
# then check the size
if len(self.cache) == self.max_conns:
for k, v in iteritems(self.timeout):
if v == self.soonest:
del self.cache[k]
del self.timeout[k]
break
self.soonest = min(self.timeout.values())
def _multiple_set_value(self, value):
turn_on = [] # transactional-ish
turn_off = [
item for item in self.items
if item.selected and (not item.disabled)
]
names = {}
for nn in value:
names[nn] = names.setdefault(nn, 0) + 1
for name, count in iteritems(names):
on, off = self._get_items(name, count)
for i in range(count):
if on:
item = on[0]
del on[0]
del turn_off[turn_off.index(item)]
else:
item = off[0]
del off[0]
turn_on.append(item)
for item in turn_off:
item.selected = False
for item in turn_on:
item.selected = True
def form_attrs_match(form_attrs):
for aname, q in iteritems(attrsq):
val = form_attrs.get(aname)
if val is None or not q(val):
return False
return True
continue
if attrs and not form_attrs_match(form.attrs):
continue
self.form = form
break # success
else:
# failure
description = []
if name is not None:
description.append("name '%s'" % name)
if predicate is not None:
description.append("predicate %s" % predicate)
if orig_nr is not None:
description.append("nr %d" % orig_nr)
if attrs:
for k, v in iteritems(attrs):
description.append('%s = %r' % (k, v))
description = ", ".join(description)
raise FormNotFoundError("no form matching " + description)
def disambiguate(items, nr, **kwds):
msgs = []
for key, value in iteritems(kwds):
msgs.append("%s=%r" % (key, value))
msg = " ".join(msgs)
if not items:
raise ItemNotFoundError(msg)
if nr is None:
if len(items) > 1:
raise AmbiguityError(msg)
nr = 0
if len(items) <= nr:
raise ItemNotFoundError(msg)
return items[nr]
continue
if attrs and not form_attrs_match(form.attrs):
continue
self.form = form
break # success
else:
# failure
description = []
if name is not None:
description.append("name '%s'" % name)
if predicate is not None:
description.append("predicate %s" % predicate)
if orig_nr is not None:
description.append("nr %d" % orig_nr)
if attrs:
for k, v in iteritems(attrs):
description.append('%s = %r' % (k, v))
description = ", ".join(description)
raise FormNotFoundError("no form matching " + description)
def __init__(self, proxies=None, proxy_bypass=None):
if proxies is None:
proxies = getproxies()
assert is_mapping(proxies), "proxies must be a mapping"
self.proxies = proxies
for type, url in iteritems(proxies):
setattr(self, '%s_open' % type,
lambda r, proxy=url, type=type, meth=self.proxy_open:
meth(r, proxy, type))
if proxy_bypass is None:
proxy_bypass = urllib_proxy_bypass
self._proxy_bypass = proxy_bypass
http_class must implement the HTTPConnection API from httplib.
The addinfourl return value is a file-like object. It also
has methods and attributes including:
- info(): return a HTTPMessage object for the headers
- geturl(): return the original request URL
- code: HTTP status code
"""
host_port = req.get_host()
if not host_port:
raise URLError('no host given')
h = http_class(host_port, timeout=req.timeout)
h.set_debuglevel(self._debuglevel)
headers = OrderedDict(req.headers)
for key, val in iteritems(req.unredirected_hdrs):
headers[key] = val
# We want to make an HTTP/1.1 request, but the addinfourl
# class isn't prepared to deal with a persistent connection.
# It will try to read all remaining data from the socket,
# which will block while the server waits for the next request.
# So make sure the connection gets closed after the (only)
# request.
headers["Connection"] = "close"
# httplib in python 2 needs str() not unicode() for all request
# parameters
if is_py2:
headers = OrderedDict(
(str(name.title()), str(val))
for name, val in iteritems(headers))
else:
headers = OrderedDict(
def find_user_password(self, realm, authuri):
attempts = [(realm, authuri), (None, authuri)]
# bleh, want default realm to take precedence over default
# URI/authority, hence this outer loop
for default_uri in False, True:
for realm, authuri in attempts:
authinfo_by_domain = self.passwd.get(realm, {})
for default_port in True, False:
reduced_authuri = self.reduce_uri(authuri, default_port)
for uri, authinfo in iteritems(authinfo_by_domain):
if uri is None and not default_uri:
continue
if self.is_suburi(uri, reduced_authuri):
return authinfo
user, password = None, None
if user is not None:
break
return user, password