Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_https_connection_read_timeout(self):
""" Handshake timeouts should fail with a Timeout"""
timed_out = Event()
def socket_handler(listener):
sock = listener.accept()[0]
while not sock.recv(65536):
pass
timed_out.wait()
sock.close()
self._start_server(socket_handler)
pool = HTTPSConnectionPool(self.host, self.port, timeout=0.001, retries=False)
try:
self.assertRaises(ReadTimeoutError, pool.request, 'GET', '/')
finally:
timed_out.set()
def test_hostname_in_first_request_packet(self):
if not HAS_SNI:
raise SkipTest('SNI-support not available')
done_receiving = Event()
self.buf = b''
def socket_handler(listener):
sock = listener.accept()[0]
self.buf = sock.recv(65536) # We only accept one packet
done_receiving.set() # let the test know it can proceed
sock.close()
self._start_server(socket_handler)
pool = HTTPSConnectionPool(self.host, self.port)
try:
pool.request('GET', '/', retries=0)
except SSLError: # We are violating the protocol
pass
done_receiving.wait()
self.assertTrue(self.host.encode() in self.buf,
"missing hostname in SSL handshake")
def test_ssl_unverified_with_ca_certs(self):
with HTTPSConnectionPool(
self.host, self.port, cert_reqs="CERT_NONE", ca_certs=DEFAULT_CA_BAD
) as pool:
with mock.patch("warnings.warn") as warn:
r = pool.request("GET", "/")
assert r.status == 200
assert warn.called
# Modern versions of Python, or systems using PyOpenSSL, only emit
# the unverified warning. Older systems may also emit other
# warnings, which we want to ignore here.
calls = warn.call_args_list
if (
sys.version_info >= (2, 7, 9)
or util.IS_PYOPENSSL
or util.IS_SECURETRANSPORT
):
def test_tunnel(self):
""" test the _tunnel behavior """
timeout = Timeout(total=None)
with HTTPSConnectionPool(
self.host, self.port, timeout=timeout, cert_reqs="CERT_NONE"
) as https_pool:
conn = https_pool._new_conn()
try:
conn.set_tunnel(self.host, self.port)
conn._tunnel = mock.Mock()
https_pool._make_request(conn, "GET", "/")
conn._tunnel.assert_called_once_with()
finally:
conn.close()
def main():
"""Main function."""
# Gather all backlink information from the rest
timestamp = datetime.datetime.now().strftime("%y-%m-%d")
stats_dir = "/popularity_stats/" + timestamp + "/"
url = 'https://127.0.0.1:45454/address/online/'
pool = urllib3.HTTPSConnectionPool("127.0.0.1", 45454, timeout=10,
cert_reqs='CERT_NONE', assert_hostname=False)
links = pool.request('GET', url).data
links = links.replace(".onion/", "").replace("http://", "").split('\n')
for onion_id in links:
try:
# Random delay 3min + 1-60 seconds
delay_time = 180 + random.randrange(1, 60)
time.sleep(delay_time)
if not onion_id:
continue
content_type = {'Content-Type':'application/json'}
onion_url = 'http://' + onion_id + '.onion/'
print onion_url
backlinks = str(get_backlinks(onion_url))
url = 'https://127.0.0.1:45454/address/' + onion_id + "/popularity/"
data = '{"date": "' + timestamp + '", "tor2web_access_count": '
super(HTTPConnection, self).__init__(*args, **kw)
class HTTPSConnection(ConnectionOverrideMixin, urllib3.connection.HTTPSConnection):
def __init__(self, *args, **kw):
self.family = _kw_scheme_to_family(kw, "https")
super(HTTPSConnection, self).__init__(*args, **kw)
# Override the higher-level `urllib3` ConnectionPool objects that instantiate
# one or more Connection objects and dispatch work between them
class HTTPConnectionPool(urllib3.HTTPConnectionPool):
ConnectionCls = HTTPConnection
class HTTPSConnectionPool(urllib3.HTTPSConnectionPool):
ConnectionCls = HTTPSConnection
# Override the highest-level `urllib3` PoolManager to also properly support the
# address family extended scheme values in URLs and pass these scheme values on
# to the individual ConnectionPool objects
class PoolManager(urllib3.PoolManager):
def __init__(self, *args, **kwargs):
super(PoolManager, self).__init__(*args, **kwargs)
# Additionally to adding our variant of the usual HTTP and HTTPS
# pool classes, also add these for some variants of the default schemes
# that are limited to some specific address family only
self.pool_classes_by_scheme = {}
for scheme, ConnectionPool in (("http", HTTPConnectionPool), ("https", HTTPSConnectionPool)):
self.pool_classes_by_scheme[scheme] = ConnectionPool
def get_json(node):
"""Send HTTP GET request to download JSON list."""
try:
http = urllib3.HTTPSConnectionPool(node, 443, timeout=10,
cert_reqs='CERT_NONE', assert_hostname=False)
response = http.request('GET', "/antanistaticmap/stats/yesterday")
except Exception as error:
print error
return ""
if response.status == 200:
return response.data
else:
return ""
def get_md5list(node):
"""Send HTTP GET request to download JSON list."""
try:
http = urllib3.HTTPSConnectionPool(node, 443, timeout=10,
cert_reqs='CERT_NONE', assert_hostname=False)
response = http.request('GET', "/antanistaticmap/lists/blacklist")
except Exception as error:
print error
return ""
if response.status == 200:
print "The list downloaded."
return response.data
else:
return ""
def analyser(json_file):
"""Analyse JSON data from Tor2web node."""
print json_file
pool = urllib3.HTTPSConnectionPool("127.0.0.1", 443, timeout=10,
cert_reqs='CERT_NONE', assert_hostname=False)
json_text = read_file(json_file)
json_data = valid_json(json_text)
if not json_data:
print "Error: %s" % json_text
raise SystemExit
dateday = json_data["date"]
if json_data:
for hidden_service in json_data["hidden_services"]:
access_count = hidden_service["access_count"]
onion_id = hidden_service["id"]
onion_url = 'http://' + onion_id + '.onion/'
print onion_url
data = '{"url": "' + onion_url + '"}'
url = 'https://127.0.0.1/address/'
content_type = {'Content-Type':'application/json'}
@property
def ok(self):
"""Return true if status code indicates success"""
return 200 <= self.status < 300
def raise_for_status(self):
"""Raise an exception for an unsuccessful HTTP status code
:raises HTTPError: if response status is not successful
"""
if not self.ok:
raise HTTPError(self.status, self)
class HTTPSConnectionPool(urllib3.HTTPSConnectionPool):
"""Connection pool providing HTTPResponse objects"""
ResponseCls = HTTPResponse
class Client(object):
"""HTTP Client for interacting with New Relic APIs
This class is used to send data to the New Relic APIs over HTTP. This class
will automatically handle retries as needed.
:param insert_key: Insights insert key
:type insert_key: str
:param host: (optional) Override the host for the client.
:type host: str
:param port: (optional) Override the port for the client.