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.
148 Examples
3
Example 1
Project: phy Source File: datasets.py
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
3
Example 2
def get(*args, **kwargs):
# bs4.find("span", {'class': 'version'})
resp = requests.head(
args[0]+"/uaa/login",
verify=False,
allow_redirects=False)
# somewhat of a hack
# pre api ops manager does not have /api/v0 endpoints
if resp.status_code == 404:
return OpsManApi(*args, **kwargs)
else:
return OpsManApi17(*args, **kwargs)
3
Example 3
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
3
Example 4
Project: kickass-get Source File: network.py
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
3
Example 5
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)
3
Example 6
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'
3
Example 7
Project: antbs Source File: monitor.py
def check_mirror_for_iso(self, version):
synced = []
for iso_pkg in status.iso_pkgs:
iso_obj = get_pkg_object(name=iso_pkg)
req = requests.head(iso_obj.iso_url, allow_redirects=True)
try:
req.raise_for_status()
synced.append(iso_obj)
except Exception as err:
logger.info(err)
if len(synced) == 2:
success = self.add_iso_versions_to_wordpress(synced)
if success:
iso_utility.clean_up_after_release(version)
self.db.delete('antbs:misc:iso-release:do_check')
else:
logger.error('At least one iso was not successfully added to wordpress.')
3
Example 8
Project: tendenci Source File: utils.py
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
3
Example 9
Project: plugin.video.emby Source File: artwork.py
def cache_texture(self, url):
# Cache a single image url to the texture cache
if url and self.enable_texture_cache:
log.debug("Processing: %s", url)
if not self.image_cache_limit:
url = self._double_urlencode(url)
try: # Add image to texture cache by simply calling it at the http endpoint
requests.head(url=("http://%s:%s/image/image://%s"
% (self.xbmc_host, self.xbmc_port, url)),
auth=(self.xbmc_username, self.xbmc_password),
timeout=(0.01, 0.01))
except Exception: # We don't need the result
pass
else:
self._add_worker_image_thread(url)
3
Example 10
Project: pep438 Source File: core.py
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
3
Example 11
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
3
Example 12
Project: open-event-orga-server Source File: import_helpers.py
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
3
Example 13
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
3
Example 14
Project: ks-email-parser Source File: gui.py
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
3
Example 15
Project: conda-manager Source File: download_api.py
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
3
Example 16
def follow_redirects():
try:
session = Session()
feeds = session.query(Feed).all()
for feed in feeds:
print('fetching', feed.feed)
try:
r = requests.head(feed.feed, allow_redirects=True)
if feed.feed != r.url:
print('urls differ', feed.feed, r.url)
feed.feed = r.url
except:
print('error', sys.exc_info()[0])
session.commit()
except:
session.rollback()
raise
finally:
session.close()
3
Example 17
Project: PyPump Source File: pypump.py
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
)
3
Example 18
Project: integration_tests Source File: test_about_links.py
@pytest.mark.tier(3)
@pytest.mark.sauce
@pytest.mark.meta(blockers=["GH#ManageIQ/manageiq:2246"])
@pytest.mark.meta(blockers=[1272618])
def test_about_links():
sel.force_navigate('about')
for link_key, link_loc in about.product_assistance.locators.items():
# If its a dict to be ver-picked and the resulting loc is None
if isinstance(link_loc, dict) and version.pick(link_loc) is None:
logger.info("Skipping link %s; not present in this version", link_key)
continue
href = sel.get_attribute(link_loc, 'href')
try:
resp = requests.head(href, verify=False, timeout=20)
except (requests.Timeout, requests.ConnectionError) as ex:
pytest.fail(str(ex))
assert 200 <= resp.status_code < 400, "Unable to access '{}' ({})".format(link_key, href)
3
Example 19
def test_connection():
import requests
try:
requests.head(FETCH_URL, timeout=5)
except:
return False
return True
3
Example 20
def remove_tile(self, tile):
if tile.coord is None:
return True
url = self.docuement_url(tile.coord)
resp = requests.head(url)
if resp.status_code == 404:
# already removed
return True
rev_id = resp.headers['etag']
url += '?rev=' + rev_id.strip('"')
self.init_db()
resp = self.req_session.delete(url)
if resp.status_code == 200:
return True
return False
3
Example 21
Project: cloudify-manager Source File: test_resources_available.py
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
3
Example 22
Project: JoomlaScan Source File: joomlascan.py
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
3
Example 23
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
3
Example 24
Project: feedhq Source File: utils.py
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
3
Example 25
Project: python-us Source File: tests.py
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)
3
Example 26
Project: doorstop Source File: client.py
def exists(path='/docuements'):
"""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
3
Example 27
Project: clusterd Source File: utility.py
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)
3
Example 28
Project: plenario Source File: views.py
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
3
Example 29
def get_metadata(self):
response = requests.head(self.url)
if response.status_code != 200:
return None
else:
return normalize_headers(response.headers)
3
Example 30
Project: SiCKRAGE Source File: test_ssl_sni.py
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
3
Example 31
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)
3
Example 32
Project: plugin.video.iplayerwww Source File: ipwww_common.py
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
3
Example 33
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
3
Example 34
Project: researchcompendia Source File: tasks.py
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)
3
Example 35
Project: redwind Source File: twitter.py
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
3
Example 36
Project: Dash.py Source File: utils.py
def resource_exist(url):
r = requests.head(url)
try:
r.raise_for_status()
return True
except requests.HTTPError:
return False
3
Example 37
def check_url(url):
try:
return bool(requests.head(url, allow_redirects=True))
except Exception as e:
print 'Error checking URL %s: %s' % (url, e)
return False
3
Example 38
Project: politwoops-tweet-collector Source File: screenshot-worker.py
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
3
Example 39
def _get_etag(self):
req = None
try:
req = requests.head(self.url)
except Exception as err:
self.logger.exception(err)
return req.headers['ETag']
3
Example 40
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)
3
Example 41
Project: cfawsinit Source File: awsdeploy.py
def wait_for_opsman_ready(inst, timeout):
addr = get_addr(inst)
def should_wait():
try:
resp = requests.head(
"https://{}/".format(addr),
verify=False, timeout=1)
return resp.status_code >= 400
except requests.exceptions.RequestException as ex:
pass
except requests.HTTPError as ex:
print ex
return True
waitFor = wait_util.wait_while(should_wait)
waitFor(timeout)
3
Example 42
def get_status(self, url):
try:
response = requests.head('http://%s' % url)
except requests.exceptions.ConnectionError:
return -1
return response.status_code
3
Example 43
Project: allura Source File: __init__.py
def oauth_has_access(self, scope):
if not scope:
return False
token = c.user.get_tool_data('GitHubProjectImport', 'token')
if not token:
return False
url = 'https://api.github.com/?access_token={}'.format(token)
r = requests.head(url)
scopes = r.headers.get('X-OAuth-Scopes', '')
scopes = [s.strip() for s in scopes.split(',')]
return scope in scopes
3
Example 44
Project: regulations-parser Source File: graphics.py
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
3
Example 45
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
3
Example 46
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)
3
Example 47
Project: waldo Source File: waldo.py
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
3
Example 48
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()
3
Example 49
Project: plenario Source File: views.py
def _assert_reachable(url):
try:
resp = requests.head(url)
assert resp.status_code != 404
except:
raise RuntimeError('Could not reach URL ' + url)
3
Example 50
Project: easywebdav Source File: __init__.py
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')