requests.head

Here are the examples of the python api requests.head taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

200 Examples 7

Example 1

Project: platformio
Source File: test_pkgmanifest.py
View license
def test_package(package_data):
    assert package_data['url'].endswith(".tar.gz")

    r = requests.head(package_data['url'], allow_redirects=True)
    validate_response(r)

    if "X-Checksum-Sha1" not in r.headers:
        return pytest.skip("X-Checksum-Sha1 is not provided")

    assert package_data['sha1'] == r.headers.get("X-Checksum-Sha1")

Example 2

Project: python-readability-api
Source File: clients.py
View license
    def head(self, url):
        """
        Make an HTTP HEAD request to the Parser API.

        :param url: url to which to make the request
        """
        logger.debug('Making HEAD request to %s', url)
        return requests.head(url)

Example 3

Project: waldo
Source File: waldo.py
View license
    def get_status(self, url):

        try:
            response = requests.head('http://%s' % url)
        except requests.exceptions.ConnectionError:
            return -1

        return response.status_code

Example 4

Project: waldo
Source File: waldo.py
View license
def run_initial_check(url):

    try:
        ip_addr = gethostbyname(url)
    except gaierror:
        error_handler('Invalid target %s' % url)

    print '[Setup] Checking %s' % url
    response = requests.head('http://%s' % url)
    # https://www.youtube.com/watch?v=3cEQX632D1M
    if response.status_code < 200 or response.status_code >= 400:
        error_handler('Invalid target %s' % url)

    print '[Setup] %s is valid... continuing' % url

    return ip_addr

Example 5

Project: researchcompendia
Source File: tasks.py
View license
def check_file_availability(file_field):
    # TODO: should it use the url attribute for FileFields rather than constructing it by hand?
    file_url = settings.S3_URL + file_field.name.lstrip('/')
    logger.debug('checking file %s', file_url)
    try:
        r = requests.head(file_url)
        if not r.ok:
            logger.critical('unavailable file. Status: %s %s URL: %s', r.status_code, status_names[r.status_code], file_url)
    except requests.exception.RequestsException:
        logger.exception('a requests exception occured while trying to access the url: %s', file_url)
    else:
        logger.info('available file. URL: %s', file_url)

Example 6

Project: BGmi
Source File: utils.py
View license
def test_connection():
    import requests

    try:
        requests.head(FETCH_URL, timeout=5)
    except:
        return False

    return True

Example 7

Project: nailgun
Source File: client.py
View license
def head(url, **kwargs):
    """A wrapper for ``requests.head``."""
    _set_content_type(kwargs)
    if _content_type_is_json(kwargs) and kwargs.get('data') is not None:
        kwargs['data'] = dumps(kwargs['data'])
    _log_request('HEAD', url, kwargs)
    response = requests.head(url, **kwargs)
    _log_response(response)
    return response

Example 8

Project: iris
Source File: test_image_json.py
View license
    def run(self):
        while not self.queue.empty():
            resource = self.queue.get()
            try:
                result = requests.head(resource)
                if result.status_code == 200:
                    self.deque.append(resource)
                else:
                    msg = '{} is not resolving correctly.'.format(resource)
                    self.exceptions.append(ValueError(msg))
            except Exception as e:
                self.exceptions.append(e)
            self.queue.task_done()

Example 9

Project: SiCKRAGE
Source File: test_ssl_sni.py
View license
def test_sni(self, provider):
    try:
        requests.head(provider.url, verify=certifi.where(), timeout=5)
    except requests.exceptions.Timeout:
        pass
    except requests.exceptions.SSLError as error:
        if 'SSL3_GET_SERVER_CERTIFICATE' not in error:
            print(error)
    except Exception:
        pass

Example 10

Project: conda-manager
Source File: download_api.py
View license
    def _is_valid_url(self, url):
        """Callback for is_valid_url."""
        try:
            r = requests.head(url, proxies=self.proxy_servers)
            value = r.status_code in [200]
        except Exception as error:
            logger.error(str(error))
            value = False

        return value

Example 11

View license
def reduce_url_list(urls):
    unique_urls = []
    for url in urls:
        if url not in unique_urls:
            try:
                response = requests.head(url, allow_redirects=True, timeout=15)
                log.info("HEAD {status_code} {url} {bytes}",
                         status_code=response.status_code,
                         url=url,
                         bytes=len(response.content) if response.content else '')
                if response.url not in unique_urls:
                    unique_urls.append(response.url)
            except requests.exceptions.SSLError as e:
                log.warning("Unable to make a HEAD request for {url} because: {e}",
                            url=url, e=e)

    return unique_urls

Example 12

Project: tendenci
Source File: utils.py
View license
def url_exists(url):
    o = urlparse(url)
    
    if not o.scheme:
        # doesn't have a scheme, relative url
        url = '%s%s' %  (get_setting('site', 'global', 'siteurl'), url)
        
    r = requests.head(url)
    return r.status_code == 200

Example 13

Project: tingbot-python
Source File: cache.py
View license
    def is_fresh(self):
        now = time.time()
        if (now-self.retrieved) < self.max_age:  # local time - local time
            return True
        try:
            response = requests.head(self.url)
            response.raise_for_status()
            old_lm = self.last_modified
            old_etag = self.etag
            self.retrieved = time.time()
            self.set_attributes(response)
            if old_etag and self.etag == old_etag:
                return True
            if old_lm and self.last_modified == old_lm:
                return True
        except IOError:
            return False
        return False

Example 14

Project: pep438
Source File: core.py
View license
def valid_package(package_name):
    """Return bool if package_name is a valid package on PyPI"""
    response = requests.head('https://pypi.python.org/pypi/%s' % package_name)
    if response.status_code != 404:
        response.raise_for_status()
    return response.status_code != 404

Example 15

Project: python-us
Source File: tests.py
View license
    def test_head(self):

        for state in us.STATES_AND_TERRITORIES:

            for region, url in state.shapefile_urls().items():
                resp = requests.head(url)
                self.assertEqual(resp.status_code, 200)

Example 16

Project: plenario
Source File: views.py
View license
def _assert_reachable(url):
    try:
        resp = requests.head(url)
        assert resp.status_code != 404
    except:
        raise RuntimeError('Could not reach URL ' + url)

Example 17

Project: plenario
Source File: views.py
View license
def is_certainly_html(url):
    head = requests.head(url)
    if head.status_code == 302:
        # Edge case with Dropbox redirects.
        return False
    try:
        return 'text/html' in head.headers['content-type']
    except KeyError:
        return False

Example 18

Project: virt-manager
Source File: urlfetcher.py
View license
    def _hasFile(self, url):
        """
        We just do a HEAD request to see if the file exists
        """
        try:
            response = requests.head(url, allow_redirects=True)
            response.raise_for_status()
        except Exception, e:
            logging.debug("HTTP hasFile request failed: %s", str(e))
            return False
        return True

Example 19

View license
def StatusBBCiD():
    r = requests.head("https://www.bbc.com/account", cookies=cookie_jar, allow_redirects=False)
    if r.status_code == 200:
        return True
    else: 
        return False

Example 20

Project: bucket3
Source File: mentions.py
View license
    def expandUrl(self, url):
        # Helper method to expand short URLs
        try:
            r = requests.head(url)
            if r.status_code in range(200, 300):
                return format(r.url)
            elif r.status_code in range(300, 400):
                # Some servers redirect http://host/path to /path/
                parts1 = urlparse(url)
                parts2 = urlparse(r.headers['location'])
                if parts2.netloc == '':
                    new_url = '%s://%s%s' % (parts1.scheme, parts1.netloc, parts2.path)
                else:
                    new_url = r.headers['location']
                return self.expandUrl(new_url)
            else:
                return format(r.status_code)
        except:
            return 'error'

Example 21

Project: Dash.py
Source File: utils.py
View license
def resource_exist(url):
    r = requests.head(url)
    try:
        r.raise_for_status()
        return True
    except requests.HTTPError:
        return False

Example 22

Project: PyPump
Source File: pypump.py
View license
    def construct_oauth_url(self):
        """ Constructs verifier OAuth URL """
        response = self._requester(requests.head, 
                                   "{0}://{1}/".format(self.protocol, self.client.server),
                                   allow_redirects=False
                                  )
        if response.is_redirect:
            server = response.headers['location']
        else:
            server = response.url

        path = "oauth/authorize?oauth_token={token}".format(
            token=self.store["oauth-request-token"]
        )
        return "{server}{path}".format(
            server=server,
            path=path
        )

Example 23

Project: python-sync-db
Source File: net.py
View license
def head_request(server_url):
    """
    Sends a HEAD request to *server_url*.

    Returns a pair of (code, reason).
    """
    if not server_url.startswith("http://") and \
            not server_url.startswith("https://"):
        server_url = "http://" + server_url
    try:
        r = requests.head(server_url, timeout=default_timeout)
        return (r.status_code, r.reason)

    except requests.exceptions.RequestException as e:
        raise NetworkError(*e.args)

    except Exception as e:
        raise NetworkError(*e.args)

Example 24

Project: xbmc-coursera
Source File: course_utils.py
View license
def getContentURL(section, cookies):
    url = section['resources']["Lecture Video"]
    if url is None:
        return None
    res = requests.head(section['resources']["Lecture Video"], cookies=cookies)
    if res.is_redirect and 'location' in res.headers:
        return res.headers['location']  # get video after another jump
    return url

Example 25

Project: regulations-parser
Source File: graphics.py
View license
    def check_for_thumb(self, url):
        thumb_url = re.sub(r'(.(png|gif|jpg))$', '.thumb' + '\\1', url)

        try:
            response = requests.head(thumb_url)
        except:
            logging.warning("Error fetching %s" % thumb_url)
            return

        if response.status_code == requests.codes.not_implemented:
            response = requests.get(thumb_url)

        if response.status_code == requests.codes.ok:
            return thumb_url

Example 26

View license
    def test_resources_available(self):
        container_ip = self.get_manager_ip()
        blueprint_id = str(uuid.uuid4())
        blueprint_name = 'empty_blueprint.yaml'
        blueprint_path = resource('dsl/{0}'.format(blueprint_name))
        self.client.blueprints.upload(blueprint_path,
                                      blueprint_id=blueprint_id)
        invalid_resource_url = 'http://{0}/resources/blueprints/{1}/{2}' \
            .format(container_ip, blueprint_id, blueprint_name)
        try:
            result = requests.head(invalid_resource_url)
            self.assertEqual(
                result.status_code, requests.status_codes.codes.not_found,
                "Resources are available through a different port than 53229.")
        except ConnectionError:
            pass

Example 27

Project: JoomlaScan
Source File: joomlascan.py
View license
def check_url_head_content_length(url, path="/"):

	fullurl = url + path
	try:
		conn = requests.head(fullurl, headers=useragentdesktop, timeout=timeoutconnection)
		return conn.headers["content-length"]
	except StandardError:
		return None

Example 28

Project: eavatar-me
Source File: client.py
View license
    def head(self, uri, params=None, headers=None):

        if headers is None:
            headers = dict()
            headers.update(self._headers)

        return requests.head(self._url + uri, params=params, headers=headers)

Example 29

Project: feedhq
Source File: utils.py
View license
def resolve_url(url):
    if settings.TESTS:
        if str(type(requests.head)) != "<class 'unittest.mock.MagicMock'>":
            raise ValueError("Not mocked")
    cache_key = 'resolve_url:{0}'.format(url)
    resolved = cache.get(cache_key)
    if resolved is None:
        resolved = url
        response = requests.head(url, headers={'User-Agent': LINK_CHECKER})
        if response.is_redirect:
            resolved = response.headers['location']
        cache.set(cache_key, resolved, 3600 * 24 * 5)
    return resolved

Example 30

View license
def is_downloadable(url):
    """
    Does the url contain a downloadable resource
    """
    h = requests.head(url, allow_redirects=True)
    header = h.headers
    content_type = header.get('content-type')
    # content_length = header.get('content-length', 1e10)
    if 'text' in content_type.lower():
        return False
    if 'html' in content_type.lower():
        return False
    return True

Example 31

Project: cum
Source File: base.py
View license
    def available(self):
        """Checks if chapter URL returns HTTP 404 or not, and returns a boolean
        value based on it. Broken links are pruned from the database.

        Some sites might not return HTTP 404 on missing chapters and require
        custom version of this method to work.
        """
        r = requests.head(self.url)
        if r.status_code == 404:
            return False
        else:
            return True

Example 32

Project: clusterd
Source File: utility.py
View license
def requests_head(*args, **kwargs):
    """ Generate a HEAD request
    """

    (args, kwargs) = build_request(args, kwargs)
    Msg("Making HEAD request to {0} with args {1}".format(args[0], kwargs),
                                                   LOG.DEBUG)
    return requests.head(*args, **kwargs)

Example 33

Project: easywebdav
Source File: __init__.py
View license
def ensure_server_initialized():
    output('Waiting for WebDAV server to start up...')
    timeout = time.time() + 20
    while True:
        if time.time() >= timeout:
            raise Exception('WebDAV server did not respond within the expected time frame')
        try:
            response = requests.head(SERVER_URL, auth=(SERVER_USERNAME, SERVER_PASSWORD))
        except requests.RequestException:
            continue
        if response.status_code < 300:
            break
        time.sleep(0.5)
    output('WebDAV server startup complete')

Example 34

Project: doorstop
Source File: client.py
View license
def exists(path='/documents'):
    """Determine if the server exists."""
    found = False
    url = utilities.build_url(path=path)
    if url:
        log.debug("looking for {}...".format(url))
        try:
            response = requests.head(url)
        except requests.exceptions.RequestException as exc:
            log.debug(exc)
        else:
            found = response.status_code == 200
        if found:
            log.info("found: {}".format(url))
    return found

Example 35

Project: LendingClub
Source File: session.py
View license
    def is_site_available(self):
        """
        Returns true if we can access LendingClub.com
        This is also a simple test to see if there's a network connection

        Returns
        -------
        boolean
            True or False
        """
        try:
            response = requests.head(self.base_url)
            status = response.status_code
            return 200 <= status < 400  # Returns true if the status code is greater than 200 and less than 400
        except Exception:
            return False

Example 36

Project: LendingClub
Source File: session.py
View license
    def is_site_available(self):
        """
        Returns true if we can access LendingClub.com
        This is also a simple test to see if there's a network connection

        Returns
        -------
        boolean
            True or False
        """
        try:
            response = requests.head(self.base_url)
            status = response.status_code
            return 200 <= status < 400  # Returns true if the status code is greater than 200 and less than 400
        except Exception:
            return False

Example 37

Project: anchore
Source File: resources.py
View license
    def get_metadata(self):
        response = requests.head(self.url)
        if response.status_code != 200:
            return None
        else:
            return normalize_headers(response.headers)

Example 38

Project: anchore
Source File: resources.py
View license
    def get_metadata(self):
        response = requests.head(self.url)
        if response.status_code != 200:
            return None
        else:
            return normalize_headers(response.headers)

Example 39

Project: kickass-get
Source File: network.py
View license
def check_connection(url):
    """ check connection to the provided url, 
        return status code
    """
    try:
        resp = requests.head(url, timeout=data.default_timeout)
        return resp.status_code
    except requests.exceptions.ConnectionError:
        return 0
    except requests.exceptions.Timeout:
        return 0

Example 40

Project: kickass-get
Source File: network.py
View license
def check_connection(url):
    """ check connection to the provided url, 
        return status code
    """
    try:
        resp = requests.head(url, timeout=data.default_timeout)
        return resp.status_code
    except requests.exceptions.ConnectionError:
        return 0
    except requests.exceptions.Timeout:
        return 0

Example 41

Project: moar
Source File: s3_storage.py
View license
    def get_thumb(self, path, key, format):
        """Get the stored thumbnail if exists.

        path:
            path of the source image
        key:
            key of the thumbnail
        format:
            thumbnail's file extension
        """
        url = self.get_url(key)
        resp = requests.head(url)
        if resp.status_code != HTTP_OK:
            return Thumb()
        return Thumb(url=url, key=key)

Example 42

Project: moar
Source File: s3_storage.py
View license
    def get_thumb(self, path, key, format):
        """Get the stored thumbnail if exists.

        path:
            path of the source image
        key:
            key of the thumbnail
        format:
            thumbnail's file extension
        """
        url = self.get_url(key)
        resp = requests.head(url)
        if resp.status_code != HTTP_OK:
            return Thumb()
        return Thumb(url=url, key=key)

Example 43

Project: ks-email-parser
Source File: gui.py
View license
    def _verify_images(self, image_path_list, verify_image_url):
        if not verify_image_url or not verify_image_url.startswith('http'):
            return image_path_list
        verified = set()
        image_url = verify_image_url.rstrip('/') + '/'
        for image_path in image_path_list:
            url = urllib.parse.urljoin(image_url, image_path)
            print('Verifying {}'.format(url))
            r = requests.head(url)
            if r.status_code == 200:
                verified.add(image_path)
        return verified

Example 44

Project: ks-email-parser
Source File: gui.py
View license
    def _verify_images(self, image_path_list, verify_image_url):
        if not verify_image_url or not verify_image_url.startswith('http'):
            return image_path_list
        verified = set()
        image_url = verify_image_url.rstrip('/') + '/'
        for image_path in image_path_list:
            url = urllib.parse.urljoin(image_url, image_path)
            print('Verifying {}'.format(url))
            r = requests.head(url)
            if r.status_code == 200:
                verified.add(image_path)
        return verified

Example 45

Project: train
Source File: ubuntu-14.04.py
View license
def get_custom(prompt):
    """Prompt for custom Docker version"""

    version = raw_input(prompt)
    if 'rc' in version:
        path = 'test.docker.com/builds/Linux/x86_64'
    elif 'dev' in version:
        path = 'master.dockerproject.org/linux/amd64'
    else:
        path = 'get.docker.com/builds/Linux/x86_64'

    r = requests.head(check_url(path, version))
    if r.status_code != 200:
        print 'Unable to locate specified version.'
        txt = get_custom('\nEnter a different version: ')
    else:
        txt = get_txt(path, version)

    return txt

Example 46

Project: train
Source File: ubuntu-14.04.py
View license
def get_custom(prompt):
    """Prompt for custom Docker version"""

    version = raw_input(prompt)
    if 'rc' in version:
        path = 'test.docker.com/builds/Linux/x86_64'
    elif 'dev' in version:
        path = 'master.dockerproject.org/linux/amd64'
    else:
        path = 'get.docker.com/builds/Linux/x86_64'

    r = requests.head(check_url(path, version))
    if r.status_code != 200:
        print 'Unable to locate specified version.'
        txt = get_custom('\nEnter a different version: ')
    else:
        txt = get_txt(path, version)

    return txt

Example 47

Project: phy
Source File: datasets.py
View license
def _remote_file_size(path):
    import requests
    try:  # pragma: no cover
        response = requests.head(path)
        return int(response.headers.get('content-length', 0))
    except Exception:
        # Unable to get the file size: no progress report.
        pass
    return 0

Example 48

Project: phy
Source File: datasets.py
View license
def _remote_file_size(path):
    import requests
    try:  # pragma: no cover
        response = requests.head(path)
        return int(response.headers.get('content-length', 0))
    except Exception:
        # Unable to get the file size: no progress report.
        pass
    return 0

Example 49

Project: redwind
Source File: twitter.py
View license
def expand_link(url):
    current_app.logger.debug('expanding %s', url)
    try:
        r = requests.head(url, allow_redirects=True, timeout=30)
        if r and r.status_code // 100 == 2:
            current_app.logger.debug('expanded to %s', r.url)
            url = r.url
    except Exception as e:
        current_app.logger.debug('request to %s failed: %s', url, e)
    return url

Example 50

Project: redwind
Source File: twitter.py
View license
def expand_link(url):
    current_app.logger.debug('expanding %s', url)
    try:
        r = requests.head(url, allow_redirects=True, timeout=30)
        if r and r.status_code // 100 == 2:
            current_app.logger.debug('expanded to %s', r.url)
            url = r.url
    except Exception as e:
        current_app.logger.debug('request to %s failed: %s', url, e)
    return url