def add_node_hook(node_ip, node_port, node_type, timeout=5):
     print("Failure 1")
     global test_no_1_success
     test_no_1_success = 0
     s = Sock()
     s.connected = 1
     return s
 def inbound_con_by_ip(ip):
     print("Failure 2")
     global test_no_1_success
     test_no_1_success = 0
     s = Sock()
     s.connected = 1
     return s
Exemple #3
0
    def __init__(self,
                 url,
                 expected_hash,
                 file_size,
                 blocking=0,
                 encoding="unicode"):
        """
        Download a file from a HTTP URL and compare it to an MD5 hash.
        Uses the sock.py module for testing.

        :param url: URL to download
        :param expected_hash: MD5 hash of file (md5sum file from term)
        :param file_size: size in bytes of the file to download
        :param blocking: use blocking or non-blocking sockets
        :return:
        """

        url = urlparse(url)
        location = url.netloc.split(":")
        if len(location) == 1:
            port = 80
            host, = location
        else:
            host, port = location

        con = Sock(host, port, blocking=0)
        req = self.build_request(host, url.path)
        con.send(req, send_all=1)
        buf = u""
        eof = u"\r\n\r\n"
        while buf != eof and con.connected:
            ch = con.recv(1)
            if len(ch):
                buf += ch

            eq = 0
            for i in range(0, len(buf)):
                if buf[i] != eof[eq]:
                    eq = 0
                else:
                    eq += 1

            # Reset buf.
            if eq == len(eof):
                break

        fp, path = tempfile.mkstemp()
        os.close(fp)
        remaining = file_size
        total = 0
        self.start_time = timer()
        with open(path, "ab") as fp:
            future = time.time() + 30  # Slow connections are slow.
            while con.connected:
                chunk_size = 2048
                if chunk_size < remaining:
                    chunk_size = remaining
                data = con.recv(chunk_size, encoding=encoding)
                if len(data):
                    remaining -= len(data)
                    total += len(data)

        found_hash = md5sum(path)
        os.remove(path)
        if expected_hash is not None:
            assert (found_hash == expected_hash)

        self.total = total
 def inbound_con_by_ip(ip):
     s = Sock()
     s.connected = 1
     return s
 def add_node_hook(node_ip, node_port, node_type, timeout=5):
     s = Sock()
     s.connected = 1
     return s