Example #1
0
    def request_and_save_img(u, p, ua, a, pause, time_out):
        start = time.time()
        opener = urllib2.build_opener()
        opener.addheaders = [('User-Agent', ua)]
        i = 0
        the_proof = True
        while i < int(a) and the_proof:
            try:
                response = opener.open(u, timeout=float(time_out))
                the_proof = False
            except urllib2.HTTPError as error:
                the_proof = True
                logging.debug('- Error at attempt number: {0} with image url: {1} -'.format(str(error.code), str(u)))
                i += 1
                time.sleep(float(pause))
        if the_proof:
            stop = time.time() - start
            message = 'Error after {0} seconds, while timeout was: {1} -\n'.format(str(stop), str(time_out))
            message += 'Error after {0} attempts, while saving image: {1} -'.format(str(a), str(u))
            logging.debug(message)
            socks.setdefaultproxy()
            EmailNotifiers.debug_error_Call(message)
            exit(1)
        html_data = response.read()

        try:
            f = open(p, 'w')
            f.write(html_data)
            f.close()
        except IOError:
            logging.info('- ERROR in request_and_save with file {0} -'.format(str(p)))