Skip to content

Commit

Permalink
Merge pull request #49 from USEPA/release-v0.3.2
Browse files Browse the repository at this point in the history
Release v0.3.2
  • Loading branch information
bl-young authored Oct 4, 2023
2 parents 56605f9 + 62d9c5e commit e6230c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
33 changes: 27 additions & 6 deletions esupy/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@
import time


headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
# ^^ HTTP 403 Error may require specifying header

def make_url_request(url, *, set_cookies=False, confirm_gdrive=False,
max_attempts=3):
max_attempts=3, **kwargs):
"""
Makes http request using requests library
:param url: URL to query
:param set_cookies:
:param confirm_gdrive:
:param max_attempts: int number of retries allowed in query
:param kwargs: pass-through to requests.Session().get()
:return: request Object
"""
session = (requests_ftp.ftp.FTPSession if urlsplit(url).scheme == 'ftp'
else requests.Session)
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
# ^^ HTTP 403 Error may require specifying header
with session() as s:
for attempt in range(max_attempts):
try:
# The session object s preserves cookies, so the second s.get()
# will have the cookies that came from the first s.get()
response = s.get(url,
# verify=False, # needed sometimes for NEI Data
)
response = s.get(url, **kwargs)
if set_cookies:
response = s.get(url)
if confirm_gdrive:
Expand Down Expand Up @@ -63,3 +63,24 @@ def make_http_request(url):
'esupy.remote.make_http_request() will be removed in the '
'future. Please modify your code accordingly.')
return make_url_request(url)


def url_is_alive(url):
"""Check that a given URL is reachable.
:param url: A URL
:rtype: bool
"""
with requests.Session() as s:
try:
response = s.get(url, headers=headers)
response.raise_for_status()
return True
except requests.exceptions.HTTPError as err:
print(err)
return False
except requests.exceptions.RequestException as err:
print(err)
return False
else:
return False
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='esupy',
version='0.3.1',
version='0.3.2',
packages=['esupy'],
include_package_data=True,
python_requires=">=3.7",
Expand Down

0 comments on commit e6230c1

Please sign in to comment.