requests.head.status_code

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

13 Examples 7

Example 1

Project: coala-bears
Source File: InvalidLinkBear.py
View license
    @staticmethod
    def get_status_code(url, timeout):
        try:
            code = requests.head(url, allow_redirects=False,
                                 timeout=timeout).status_code
            return code
        except requests.exceptions.RequestException:
            pass

Example 2

Project: jarbas
Source File: models.py
View license
    def fetch_url(self):
        if self.url:
            return self.url

        probable_url = self.get_url()
        status = head(probable_url).status_code
        url = probable_url if 200 <= status < 400 else None

        self.url = url
        self.fetched = True
        self.save()

        return url

Example 3

Project: landsat-util
Source File: downloader.py
View license
    def remote_file_exists(self, url):
        """ Checks whether the remote file exists.

        :param url:
            The url that has to be checked.
        :type url:
            String

        :returns:
            **True** if remote file exists and **False** if it doesn't exist.
        """
        status = requests.head(url).status_code

        if status != 200:
            raise RemoteFileDoesntExist

Example 4

Project: django-embed-video
Source File: backends.py
View license
    def get_thumbnail_url(self):
        """
        Returns thumbnail URL folded from :py:data:`pattern_thumbnail_url` and
        parsed code.

        :rtype: str
        """
        for resolution in self.resolutions:
            temp_thumbnail_url = self.pattern_thumbnail_url.format(
                code=self.code, protocol=self.protocol, resolution=resolution)
            if int(requests.head(temp_thumbnail_url).status_code) < 400:
                return temp_thumbnail_url
        return None

Example 5

Project: integration_tests
Source File: test_docs.py
View license
@pytest.mark.tier(2)
@pytest.mark.ignore_stream("upstream")
@pytest.mark.sauce
def test_links(guides, soft_assert):
    """Test whether the PDF documents are present."""
    navigate_to(current_appliance.server, 'Documentation')
    for link in guides:
        locator = getattr(about, link)
        url = pytest.sel.get_attribute(locator, "href")
        soft_assert(
            requests.head(url, verify=False).status_code == 200,
            "'{}' is not accessible".format(pytest.sel.text(locator).encode("utf-8").strip())
        )

Example 6

Project: integration_tests
Source File: test_docs.py
View license
@pytest.mark.tier(2)
@pytest.mark.ignore_stream("upstream")
@pytest.mark.sauce
def test_links(guides, soft_assert):
    """Test whether the PDF documents are present."""
    navigate_to(current_appliance.server, 'Documentation')
    for link in guides:
        locator = getattr(about, link)
        url = pytest.sel.get_attribute(locator, "href")
        soft_assert(
            requests.head(url, verify=False).status_code == 200,
            "'{}' is not accessible".format(pytest.sel.text(locator).encode("utf-8").strip())
        )

Example 7

Project: unisubs
Source File: http.py
View license
def url_exists(url):
    """Check that a url (when following redirection) exists.

    This is needed because Django's validators rely on Python's urllib2 which in
    verions < 2.6 won't follow redirects.

    """
    try:
        return 200 <= requests.head(url, timeout=15.0).status_code < 400
    except (requests.ConnectionError, requests.Timeout):
        return False

Example 8

View license
def main(options):
    """Main entry point for the script."""
    bb_url = "https://api.bitbucket.org/1.0/repositories/{repo}/issues".format(
        repo=options.bitbucket_repo)
    options.bb_auth = None
    bb_repo_status = requests.head(bb_url).status_code
    if bb_repo_status == 404:
        raise RuntimeError(
            "Could not find a Bitbucket Issue Tracker at: {}\n"
            "Hint: the Bitbucket repository name is case-sensitive."
            .format(bb_url)
        )
    elif bb_repo_status == 403:  # Only need BB auth creds for private BB repos
        if not options.bitbucket_username:
            raise RuntimeError(
                """
                Trying to access a private Bitbucket repository, but no
                Bitbucket username was entered. Please rerun the script using
                the argument `--bb_user <username>` to pass in your Bitbucket
                username.
                """
            )
        kr_pass_bb = keyring.get_password('Bitbucket', options.bitbucket_username)
        bitbucket_password = kr_pass_bb or getpass.getpass(
            "Please enter your Bitbucket password.\n"
            "Note: If your Bitbucket account has two-factor authentication "
            "enabled, you must temporarily disable it until "
            "https://bitbucket.org/site/master/issues/11774/ is resolved.\n"
        )
        options.bb_auth = (options.bitbucket_username, bitbucket_password)
        # Verify BB creds work
        bb_creds_status = requests.head(bb_url, auth=options.bb_auth).status_code
        if bb_creds_status == 401:
            raise RuntimeError("Failed to login to Bitbucket.")
        elif bb_creds_status == 403:
            raise RuntimeError(
                "Bitbucket login succeeded, but user '{}' doesn't have "
                "permission to access the url: {}"
                .format(options.bitbucket_username, bb_url)
            )

    # Always need the GH pass so format_user() can verify links to GitHub user
    # profiles don't 404. Auth'ing necessary to get higher GH rate limits.
    kr_pass_gh = keyring.get_password('Github', options.github_username)
    github_password = kr_pass_gh or getpass.getpass(
        "Please enter your GitHub password.\n"
        "Note: If your GitHub account has authentication enabled, "
        "you must use a personal access token from "
        "https://github.com/settings/tokens in place of a password for this "
        "script.\n"
    )
    options.gh_auth = (options.github_username, github_password)
    # Verify GH creds work
    gh_repo_url = 'https://api.github.com/repos/' + options.github_repo
    gh_repo_status = requests.head(gh_repo_url, auth=options.gh_auth).status_code
    if gh_repo_status == 401:
        raise RuntimeError("Failed to login to GitHub.")
    elif gh_repo_status == 403:
        raise RuntimeError(
            "GitHub login succeeded, but user '{}' either doesn't have "
            "permission to access the repo at: {}\n"
            "or is over their GitHub API rate limit.\n"
            "You can read more about GitHub's API rate limiting policies here: "
            "https://developer.github.com/v3/#rate-limiting"
            .format(options.github_username, gh_repo_url)
        )
    elif gh_repo_status == 404:
        raise RuntimeError("Could not find a GitHub repo at: " + gh_repo_url)

    issues = get_issues(bb_url, options.start, options.bb_auth)
    for index, issue in enumerate(issues):
        comments = get_issue_comments(issue['local_id'], bb_url, options.bb_auth)
        gh_issue = convert_issue(issue, options)
        gh_comments = [convert_comment(c, options) for c in comments
                       if convert_comment(c, options) is not None]

        if options.dry_run:
            print("\nIssue: ", gh_issue)
            print("\nComments: ", gh_comments)
        else:
            # GitHub's Import API currently requires a special header
            headers = {'Accept': 'application/vnd.github.golden-comet-preview+json'}
            push_respo = push_github_issue(
                gh_issue, gh_comments, options.github_repo, options.gh_auth, headers
            )
            # issue POSTed successfully, now verify the import finished before
            # continuing. Otherwise, we risk issue IDs not being sync'd between
            # Bitbucket and GitHub because GitHub processes the data in the
            # background, so IDs can be out of order if two issues are POSTed
            # and the latter finishes before the former. For example, if the
            # former had a bunch more comments to be processed.
            # https://github.com/jeffwidman/bitbucket-issue-migration/issues/45
            status_url = push_respo.json()['url']
            gh_issue_url = verify_github_issue_import_finished(
                status_url, options.gh_auth, headers).json()['issue_url']
            # verify GH & BB issue IDs match
            # if this fails, convert_links() will have incorrect output
            # this will fail if the GH repository has pre-existing issues
            gh_issue_id = int(gh_issue_url.split('/')[-1])
            assert gh_issue_id == issue['local_id']
        print("Completed {} of {} issues".format(index + 1, len(issues)))

Example 9

View license
def main(options):
    """Main entry point for the script."""
    bb_url = "https://api.bitbucket.org/1.0/repositories/{repo}/issues".format(
        repo=options.bitbucket_repo)
    options.bb_auth = None
    bb_repo_status = requests.head(bb_url).status_code
    if bb_repo_status == 404:
        raise RuntimeError(
            "Could not find a Bitbucket Issue Tracker at: {}\n"
            "Hint: the Bitbucket repository name is case-sensitive."
            .format(bb_url)
        )
    elif bb_repo_status == 403:  # Only need BB auth creds for private BB repos
        if not options.bitbucket_username:
            raise RuntimeError(
                """
                Trying to access a private Bitbucket repository, but no
                Bitbucket username was entered. Please rerun the script using
                the argument `--bb_user <username>` to pass in your Bitbucket
                username.
                """
            )
        kr_pass_bb = keyring.get_password('Bitbucket', options.bitbucket_username)
        bitbucket_password = kr_pass_bb or getpass.getpass(
            "Please enter your Bitbucket password.\n"
            "Note: If your Bitbucket account has two-factor authentication "
            "enabled, you must temporarily disable it until "
            "https://bitbucket.org/site/master/issues/11774/ is resolved.\n"
        )
        options.bb_auth = (options.bitbucket_username, bitbucket_password)
        # Verify BB creds work
        bb_creds_status = requests.head(bb_url, auth=options.bb_auth).status_code
        if bb_creds_status == 401:
            raise RuntimeError("Failed to login to Bitbucket.")
        elif bb_creds_status == 403:
            raise RuntimeError(
                "Bitbucket login succeeded, but user '{}' doesn't have "
                "permission to access the url: {}"
                .format(options.bitbucket_username, bb_url)
            )

    # Always need the GH pass so format_user() can verify links to GitHub user
    # profiles don't 404. Auth'ing necessary to get higher GH rate limits.
    kr_pass_gh = keyring.get_password('Github', options.github_username)
    github_password = kr_pass_gh or getpass.getpass(
        "Please enter your GitHub password.\n"
        "Note: If your GitHub account has authentication enabled, "
        "you must use a personal access token from "
        "https://github.com/settings/tokens in place of a password for this "
        "script.\n"
    )
    options.gh_auth = (options.github_username, github_password)
    # Verify GH creds work
    gh_repo_url = 'https://api.github.com/repos/' + options.github_repo
    gh_repo_status = requests.head(gh_repo_url, auth=options.gh_auth).status_code
    if gh_repo_status == 401:
        raise RuntimeError("Failed to login to GitHub.")
    elif gh_repo_status == 403:
        raise RuntimeError(
            "GitHub login succeeded, but user '{}' either doesn't have "
            "permission to access the repo at: {}\n"
            "or is over their GitHub API rate limit.\n"
            "You can read more about GitHub's API rate limiting policies here: "
            "https://developer.github.com/v3/#rate-limiting"
            .format(options.github_username, gh_repo_url)
        )
    elif gh_repo_status == 404:
        raise RuntimeError("Could not find a GitHub repo at: " + gh_repo_url)

    issues = get_issues(bb_url, options.start, options.bb_auth)
    for index, issue in enumerate(issues):
        comments = get_issue_comments(issue['local_id'], bb_url, options.bb_auth)
        gh_issue = convert_issue(issue, options)
        gh_comments = [convert_comment(c, options) for c in comments
                       if convert_comment(c, options) is not None]

        if options.dry_run:
            print("\nIssue: ", gh_issue)
            print("\nComments: ", gh_comments)
        else:
            # GitHub's Import API currently requires a special header
            headers = {'Accept': 'application/vnd.github.golden-comet-preview+json'}
            push_respo = push_github_issue(
                gh_issue, gh_comments, options.github_repo, options.gh_auth, headers
            )
            # issue POSTed successfully, now verify the import finished before
            # continuing. Otherwise, we risk issue IDs not being sync'd between
            # Bitbucket and GitHub because GitHub processes the data in the
            # background, so IDs can be out of order if two issues are POSTed
            # and the latter finishes before the former. For example, if the
            # former had a bunch more comments to be processed.
            # https://github.com/jeffwidman/bitbucket-issue-migration/issues/45
            status_url = push_respo.json()['url']
            gh_issue_url = verify_github_issue_import_finished(
                status_url, options.gh_auth, headers).json()['issue_url']
            # verify GH & BB issue IDs match
            # if this fails, convert_links() will have incorrect output
            # this will fail if the GH repository has pre-existing issues
            gh_issue_id = int(gh_issue_url.split('/')[-1])
            assert gh_issue_id == issue['local_id']
        print("Completed {} of {} issues".format(index + 1, len(issues)))

Example 10

View license
def format_user(user, gh_auth):
    """
    Format a Bitbucket user's info into a string containing either 'Anonymous'
    or their name and links to their Bitbucket and GitHub profiles.

    The GitHub profile link may be incorrect because it assumes they reused
    their Bitbucket username on GitHub.
    """
    # anonymous comments have null 'author_info', anonymous issues don't have
    # 'reported_by' key, so just be sure to pass in None
    if user is None:
        return "Anonymous"
    bb_user = "Bitbucket: [{0}](https://bitbucket.org/{0})".format(user['username'])
    # Verify GH user link doesn't 404. Unfortunately can't use
    # https://github.com/<name> because it might be an organization
    gh_user_url = ('https://api.github.com/users/' + user['username'])
    status_code = requests.head(gh_user_url, auth=gh_auth).status_code
    if status_code == 200:
        gh_user = "GitHub: [{0}](https://github.com/{0})".format(user['username'])
    elif status_code == 404:
        gh_user = "GitHub: Unknown"
    elif status_code == 403:
        raise RuntimeError(
            "GitHub returned HTTP Status Code 403 Forbidden when accessing: {}."
            "\nThis may be due to rate limiting.\n"
            "You can read more about GitHub's API rate limiting policies here: "
            "https://developer.github.com/v3/#rate-limiting"
            .format(gh_user_url)
        )
    else:
        raise RuntimeError(
            "Failed to check GitHub User url: {} due to "
            "unexpected HTTP status code: {}"
            .format(gh_user_url, status_code)
        )
    return (user['display_name'] + " (" + bb_user + ", " + gh_user + ")")

Example 11

View license
def format_user(user, gh_auth):
    """
    Format a Bitbucket user's info into a string containing either 'Anonymous'
    or their name and links to their Bitbucket and GitHub profiles.

    The GitHub profile link may be incorrect because it assumes they reused
    their Bitbucket username on GitHub.
    """
    # anonymous comments have null 'author_info', anonymous issues don't have
    # 'reported_by' key, so just be sure to pass in None
    if user is None:
        return "Anonymous"
    bb_user = "Bitbucket: [{0}](https://bitbucket.org/{0})".format(user['username'])
    # Verify GH user link doesn't 404. Unfortunately can't use
    # https://github.com/<name> because it might be an organization
    gh_user_url = ('https://api.github.com/users/' + user['username'])
    status_code = requests.head(gh_user_url, auth=gh_auth).status_code
    if status_code == 200:
        gh_user = "GitHub: [{0}](https://github.com/{0})".format(user['username'])
    elif status_code == 404:
        gh_user = "GitHub: Unknown"
    elif status_code == 403:
        raise RuntimeError(
            "GitHub returned HTTP Status Code 403 Forbidden when accessing: {}."
            "\nThis may be due to rate limiting.\n"
            "You can read more about GitHub's API rate limiting policies here: "
            "https://developer.github.com/v3/#rate-limiting"
            .format(gh_user_url)
        )
    else:
        raise RuntimeError(
            "Failed to check GitHub User url: {} due to "
            "unexpected HTTP status code: {}"
            .format(gh_user_url, status_code)
        )
    return (user['display_name'] + " (" + bb_user + ", " + gh_user + ")")

Example 12

Project: kolibri
Source File: api.py
View license
    @list_route(methods=['post'])
    def startremoteimport(self, request):
        '''Download a channel's database from the main curation server, and then
        download its content.

        '''
        TASKTYPE = "remoteimport"

        if "channel_id" not in request.data:
            raise serializers.ValidationError("The 'channel_id' field is required.")

        channel_id = request.data['channel_id']

        # ensure the requested channel_id can be found on the central server, otherwise error
        status = requests.head(get_content_database_file_url(channel_id)).status_code
        if status == 404:
            raise Http404(_("The requested channel does not exist on the content server."))

        task_id = make_async_call(_networkimport, channel_id, group=TASKTYPE, progress_updates=True)

        # attempt to get the created Task, otherwise return pending status
        resp = _task_to_response(Task.get_task(task_id), task_type=TASKTYPE, task_id=task_id)

        return Response(resp)

Example 13

Project: kolibri
Source File: api.py
View license
    @list_route(methods=['post'])
    def startremoteimport(self, request):
        '''Download a channel's database from the main curation server, and then
        download its content.

        '''
        TASKTYPE = "remoteimport"

        if "channel_id" not in request.data:
            raise serializers.ValidationError("The 'channel_id' field is required.")

        channel_id = request.data['channel_id']

        # ensure the requested channel_id can be found on the central server, otherwise error
        status = requests.head(get_content_database_file_url(channel_id)).status_code
        if status == 404:
            raise Http404(_("The requested channel does not exist on the content server."))

        task_id = make_async_call(_networkimport, channel_id, group=TASKTYPE, progress_updates=True)

        # attempt to get the created Task, otherwise return pending status
        resp = _task_to_response(Task.get_task(task_id), task_type=TASKTYPE, task_id=task_id)

        return Response(resp)