当前位置: 首页>>代码示例>>Python>>正文


Python requests.head方法代码示例

本文整理汇总了Python中requests.head方法的典型用法代码示例。如果您正苦于以下问题:Python requests.head方法的具体用法?Python requests.head怎么用?Python requests.head使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在requests的用法示例。


在下文中一共展示了requests.head方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _get_wp_api_url

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def _get_wp_api_url(self, url):
        """
        Private function for finding the WP-API URL.

        Arguments
        ---------

        url : str
            WordPress instance URL.
        """
        resp = requests.head(url)

        # Search the Links for rel="https://api.w.org/".
        wp_api_rel = resp.links.get('https://api.w.org/')

        if wp_api_rel:
            return wp_api_rel['url']
        else:
            # TODO: Rasie a better exception to the rel doesn't exist.
            raise Exception 
开发者ID:myles,项目名称:python-wp,代码行数:22,代码来源:api.py

示例2: main

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def main():
    print('hello world')
    print('contacting google.com...')
    r = requests.head("https://www.google.com")
    print("status code:", r.status_code)
    print("PATH:", os.getenv("PATH"))
    result = helper.add(1, 1)
    print(f"1 + 1 = {result}")
    try:
        with open(Path(BASE_DIR, "input.txt")) as f:
            content = f.read().strip()
        print(content)
    except IOError:
        print("Warning: input.txt was not found")
    #
    fname = Path(BASE_DIR, "output.txt")
    print("writing to the following file:", fname)
    with open(fname, "w") as g:
        print("writing to a file", file=g)

############################################################################## 
开发者ID:jabbalaci,项目名称:PythonEXE,代码行数:23,代码来源:hello.py

示例3: download_file

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def download_file(url, filename):
    # NOTE the stream=True parameter
    # response = requests.head(url)

    # if "Content-Length" in response.headers:
    #     print("length---" + response.headers["Content-Length"])
    # if "Content-Length" in response.headers and int(response.headers["Content-Length"]) > 100000:
    #     r = requests.get(url, stream=True)
    #     with open(filename, 'wb') as f:
    #         for chunk in r.iter_content(chunk_size=1024):
    #             if chunk:  # filter out keep-alive new chunks
    #                 f.write(chunk)
    #                 f.flush()
    # return filename
    r = requests.get(url, stream=True)
    if len(r.content) > 50000:
        # print("img url----" + url + "     " + str(len(r.content)))
        with open(filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:  # filter out keep-alive new chunks
                    f.write(chunk)
                    f.flush()
        return True
    else:
        return False 
开发者ID:cymcsg,项目名称:Awesome-Mobile-UI,代码行数:27,代码来源:generate.py

示例4: status_select

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def status_select(driver, url, status='hide'):
    '''
    For now it doesnt do what its name suggests, 
    I have planned to add a status reporter of the http response code.
    This part of the code is not removed because it is part of its core.
    Treat it like it isnt here.
    '''
    try:
        if status == 'hide':
            driver.get(url)
        elif status == 'show':
            r = requests.head(url)
            if r.status_code == 503:
                raise RuntimeError("This website's sevice is unavailable or has cloudflare on.")
            driver.get(url)
            return r.status_code
        else:
            driver.get(url)
    except requests.ConnectionError:
        raise RuntimeError("Failed to establish a connection using the requests library.") 
开发者ID:vn-ki,项目名称:anime-downloader,代码行数:22,代码来源:selescrape.py

示例5: _download_report

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def _download_report(source_url, destination_file, chunk_size):
        response = requests.head(source_url)
        content_length = int(response.headers['Content-Length'])

        start_byte = 0
        while start_byte < content_length:
            end_byte = start_byte + chunk_size - 1
            if end_byte >= content_length:
                end_byte = content_length - 1

            headers = {'Range': 'bytes=%s-%s' % (start_byte, end_byte)}
            response = requests.get(source_url, stream=True, headers=headers)
            chunk = response.raw.read()
            destination_file.write(chunk)
            start_byte = end_byte + 1
        destination_file.close() 
开发者ID:google,项目名称:orchestra,代码行数:18,代码来源:display_video_360.py

示例6: checkUrl

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def checkUrl(url):

    # check
    try:
        ress1 = requests.head(url , allow_redirects=True)

        if url != ress1.url:
            return "Maybe you should use ;"+ress1.url
        else:
            ress = requests.get(url)
            code = ress.status_code
            if (code == 200):
                return True
            else:
               return False
    except requests.exceptions.ConnectionError:
        return "Try a different url please"
    except requests.exceptions.MissingSchema:
        return "Try a different url please"
    except:
        return False

## make input work both in python3 and 2 
开发者ID:abaykan,项目名称:CrawlBox,代码行数:25,代码来源:crawlbox.py

示例7: cargoport_url

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def cargoport_url(self):
        """
        Returns the Galaxy cargo-port URL for this version of this package, if
        it exists.
        """
        url = cargoport_url(self.package, self.version, self.bioc_version)
        response = requests.head(url)
        if response.status_code == 404:
            # This is expected if this is a new package or an updated version.
            # Cargo Port will archive a working URL upon merging
            return
        elif response.status_code == 200:
            return url
        else:
            raise PageNotFoundError(
                "Unexpected error: {0.status_code} ({0.reason})".format(response)) 
开发者ID:bioconda,项目名称:bioconda-utils,代码行数:18,代码来源:bioconductor_skeleton.py

示例8: tarball_url

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def tarball_url(self):
        if not self._tarball_url:
            urls = [self.bioconductor_tarball_url,
                    self.bioarchive_url,
                    self.cargoport_url]
            for url in urls:
                if url is not None:
                    response = requests.head(url)
                    if response.status_code == 200:
                        self._tarball_url = url
                        return url

            logger.error(
                'No working URL for %s==%s in Bioconductor %s. '
                'Tried the following:\n\t' + '\n\t'.join(urls),
                self.package, self.version, self.bioc_version
            )

            if self._auto:
                find_best_bioc_version(self.package, self.version)

            if self._tarball_url is None:
                raise ValueError(
                    "No working URLs found for this version in any bioconductor version")
        return self._tarball_url 
开发者ID:bioconda,项目名称:bioconda-utils,代码行数:27,代码来源:bioconductor_skeleton.py

示例9: check

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def check(out, url):
	printer("Testing: " + url)
	url = 'http://' + url
	try:
		req = requests.head(url, timeout=10)
		scode = str(req.status_code)
		if scode.startswith("2"):
			print(green + "[+] "+scode+" | Found: " + end + "[ " + url + " ]")
		elif scode.startswith("3"):
			link = req.headers['Location']
			print(yellow + "[*] "+scode+" | Redirection: " + end + "[ " + url + " ]" + yellow + " -> " + end + "[ " + link + " ]")
		elif st.startswith("4"):
			print(blue+"[!] "+scode+" | Check: " + end + "[ " + url + " ]")

		if out != 'None':
			with open(out, 'a') as f:
				f.write(url+"\n")
				f.close()

		return True

	except Exception:
		return False 
开发者ID:bing0o,项目名称:Python-Scripts,代码行数:25,代码来源:subchecker.py

示例10: support_continue

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def support_continue(self, url):
        '''
            check support continue download or not
        '''
        headers = {'Range': 'bytes=0-4'}
        try:
            r = requests.head(url, headers=headers)
            if 'content-range' in r.headers:
                crange = r.headers['content-range']
                self.total = int(re.match(ur'^bytes 0-4/(\d+)$', crange).group(1))
                self.songObj.sizeUpdated.emit(self.total)
                return True
            else:
                self.total = 0
                return False
        except:
            logger.error(traceback.print_exc())
        try:
            self.total = int(r.headers['content-length'])
            self.songObj.sizeUpdated.emit(self.total)
        except:
            logger.error(traceback.print_exc())
            self.total = 0
        return False 
开发者ID:dragondjf,项目名称:QMusic,代码行数:26,代码来源:downloadsongworker.py

示例11: _download

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def _download(url, path, overwrite=False, sizecompare=False):
    ''' Download from internet'''
    if os.path.isfile(path) and not overwrite:
        if sizecompare:
            file_size = os.path.getsize(path)
            res_head = requests.head(url)
            res_get = requests.get(url, stream=True)
            if 'Content-Length' not in res_head.headers:
                res_get = urllib2.urlopen(url)
            urlfile_size = int(res_get.headers['Content-Length'])
            if urlfile_size != file_size:
                print("exist file got corrupted, downloading", path, " file freshly")
                _download(url, path, True, False)
                return
        print('File {} exists, skip.'.format(path))
        return
    print('Downloading from url {} to {}'.format(url, path))
    try:
        urllib.request.urlretrieve(url, path)
        print('')
    except:
        urllib.urlretrieve(url, path) 
开发者ID:mlperf,项目名称:training_results_v0.6,代码行数:24,代码来源:test_forward.py

示例12: check_project_exist

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def check_project_exist(self, project_name):
        result = False
        path = '%s://%s:%s/api/projects?project_name=%s' % (
            self.protocol, self.host, self.port,project_name)
        response = requests.head(path,
                                 cookies={'beegosessionID': self.session_id}, verify=False)
        if response.status_code == 200:
            result = True
            logger.debug(
                "Successfully check project exist, result: {}".format(result))
        elif response.status_code == 404:
            result = False
            logger.debug(
                "Successfully check project exist, result: {}".format(result))
        else:
            logger.error("Fail to check project exist")
        return result

    # POST /projects 
开发者ID:bbotte,项目名称:bbotte.github.io,代码行数:21,代码来源:harborclient.py

示例13: initialize

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def initialize(self):
        '''List all datasets for a given ...'''
        fmt = guess_format(self.source.url)
        # if format can't be guessed from the url
        # we fallback on the declared Content-Type
        if not fmt:
            response = requests.head(self.source.url)
            mime_type = response.headers.get('Content-Type', '').split(';', 1)[0]
            if not mime_type:
                msg = 'Unable to detect format from extension or mime type'
                raise ValueError(msg)
            fmt = guess_format(mime_type)
            if not fmt:
                msg = 'Unsupported mime type "{0}"'.format(mime_type)
                raise ValueError(msg)
        graph = self.parse_graph(self.source.url, fmt)
        self.job.data = {'graph': graph.serialize(format='json-ld', indent=None)} 
开发者ID:opendatateam,项目名称:udata,代码行数:19,代码来源:dcat.py

示例14: checkBucketWithoutCreds

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def checkBucketWithoutCreds(bucketName, triesLeft=2):
    """ Does a simple GET request with the Requests library and interprets the results.
    bucketName - A domain name without protocol (http[s]) """

    if triesLeft == 0:
        return False

    bucketUrl = 'http://' + bucketName + '.s3.amazonaws.com'

    r = requests.head(bucketUrl)

    if r.status_code == 200:    # Successfully found a bucket!
        return True
    elif r.status_code == 403:  # Bucket exists, but we're not allowed to LIST it.
        return True
    elif r.status_code == 404:  # This is definitely not a valid bucket name.
        return False
    elif r.status_code == 503:
        return checkBucketWithoutCreds(bucketName, triesLeft - 1)
    else:
        raise ValueError("Got an unhandled status code back: " + str(r.status_code) + " for bucket: " + bucketName +
                         ". Please open an issue at: https://github.com/sa7mon/s3scanner/issues and include this info.") 
开发者ID:sa7mon,项目名称:S3Scanner,代码行数:24,代码来源:s3utils.py

示例15: _scan_target_normal

# 需要导入模块: import requests [as 别名]
# 或者: from requests import head [as 别名]
def _scan_target_normal(self,target):
        try:
            r=requests.head(target)
            #it maybe a directory
            if self._check_eponymous_dir(r,target):
                return target+'/;'

            if self._check_exist_code(r.status_code):
                # check content
                r=requests.get(target)
                if self._check_exist_code(r.status_code):
                    for cur in self._not_exist_flag:
                        if r.text.find(cur)!=-1:
                            return ''
                    return target+';'
            return ''
        except Exception as e:
            return '' 
开发者ID:a7vinx,项目名称:swarm,代码行数:20,代码来源:dirsc.py


注:本文中的requests.head方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。