本文整理汇总了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
示例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)
##############################################################################
示例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
示例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.")
示例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()
示例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
示例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))
示例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
示例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
示例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
示例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)
示例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
示例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)}
示例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.")
示例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 ''