Skip to content

Commit 40479bf

Browse files
authored
Merge branch 'master' into pandas-concatenation-update
2 parents dea8de5 + f837e3f commit 40479bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1362
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Build a Site Connectivity Checker in Python
2+
3+
RP Checker is a site connectivity checker utility. It takes one or more website URLs and checks if those sites are online. It can perform the connectivity checks either synchronously or asynchronously.
4+
5+
## Installation
6+
7+
1. Create a Python virtual environment
8+
9+
```sh
10+
$ python -m venv ./venv
11+
$ source venv/bin/activate
12+
(venv) $
13+
```
14+
15+
2. Install the requirements
16+
17+
```
18+
(venv) $ python -m pip install -r requirements.txt
19+
```
20+
21+
## Run the Project
22+
23+
```sh
24+
(venv) $ python -m rpchecker -u python.org
25+
The status of "python.org" is: "Online!" 👍
26+
```
27+
28+
## Features
29+
30+
RP Checker provides the following options:
31+
32+
- `-u` or `--urls` takes one or more URLs and checks if they're online.
33+
- `-f` or `--input-file` takes a file containing a list of URLs to check.
34+
- `-a` or `--asynchronous` runs the check asynchronously.
35+
36+
## About the Author
37+
38+
Leodanis Pozo Ramos - Email: [email protected]
39+
40+
## License
41+
42+
Distributed under the MIT license. See `LICENSE` in the root directory of this `materials` repo for more information.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
python.org
2+
pypi.org
3+
docs.python.org
4+
peps.python.org
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Build a Site Connectivity Checker in Python
2+
3+
RP Checker is a site connectivity checker utility. It takes one or more website URLs and checks if those sites are online. It can perform the connectivity checks either synchronously or asynchronously.
4+
5+
## Installation
6+
7+
1. Create a Python virtual environment
8+
9+
```sh
10+
$ python -m venv ./venv
11+
$ source venv/bin/activate
12+
(venv) $
13+
```
14+
15+
2. Install the requirements
16+
17+
```
18+
(venv) $ python -m pip install -r requirements.txt
19+
```
20+
21+
## Run the Project
22+
23+
```sh
24+
(venv) $ python -m rpchecker -u python.org
25+
The status of "python.org" is: "Online!" 👍
26+
```
27+
28+
## Features
29+
30+
RP Checker provides the following options:
31+
32+
- `-u` or `--urls` takes one or more URLs and checks if they're online.
33+
- `-f` or `--input-file` takes a file containing a list of URLs to check.
34+
- `-a` or `--asynchronous` runs the check asynchronously.
35+
36+
## About the Author
37+
38+
Leodanis Pozo Ramos - Email: [email protected]
39+
40+
## License
41+
42+
Distributed under the MIT license. See `LICENSE` in the root directory of this `materials` repo for more information.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aiohttp==3.7.4.post0
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""Top-level package rpchecker."""
2+
3+
__version__ = "0.1.0"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""This module provides the RP Checker entry point script."""
2+
3+
import asyncio
4+
import pathlib
5+
import sys
6+
7+
from rpchecker.checker import site_is_online, site_is_online_async
8+
from rpchecker.cli import display_check_result, read_user_cli_args
9+
10+
11+
def main():
12+
"""Run RP Checker."""
13+
user_args = read_user_cli_args()
14+
urls = _get_websites_urls(user_args)
15+
if not urls:
16+
print("Error: no URLs to check", file=sys.stderr)
17+
sys.exit(1)
18+
19+
if user_args.asynchronous:
20+
asyncio.run(_asynchronous_check(urls))
21+
else:
22+
_synchronous_check(urls)
23+
24+
25+
def _get_websites_urls(user_args):
26+
urls = user_args.urls
27+
if user_args.input_file:
28+
urls += _read_urls_from_file(user_args.input_file)
29+
return urls
30+
31+
32+
def _read_urls_from_file(file):
33+
file_path = pathlib.Path(file)
34+
if file_path.is_file():
35+
with file_path.open() as urls_file:
36+
urls = [url.strip() for url in urls_file]
37+
if urls:
38+
return urls
39+
print(f"Error: empty input file, {file}", file=sys.stderr)
40+
else:
41+
print("Error: input file not found", file=sys.stderr)
42+
return []
43+
44+
45+
async def _asynchronous_check(urls):
46+
async def _check(url):
47+
error = ""
48+
try:
49+
result = await site_is_online_async(url)
50+
except Exception as e:
51+
result = False
52+
error = str(e)
53+
display_check_result(result, url, error)
54+
55+
await asyncio.gather(*(_check(url) for url in urls))
56+
57+
58+
def _synchronous_check(urls):
59+
for url in urls:
60+
error = ""
61+
try:
62+
result = site_is_online(url)
63+
except Exception as e:
64+
result = False
65+
error = str(e)
66+
display_check_result(result, url, error)
67+
68+
69+
if __name__ == "__main__":
70+
main()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import asyncio
2+
from http.client import HTTPConnection
3+
from urllib.parse import urlparse
4+
5+
import aiohttp
6+
7+
8+
def site_is_online(url, timeout=2):
9+
"""Return True if the target URL is online.
10+
11+
Raise an exception otherwise.
12+
"""
13+
error = Exception("unknown error")
14+
parser = urlparse(url)
15+
host = parser.netloc or parser.path.split("/")[0]
16+
for port in (80, 443):
17+
connection = HTTPConnection(host=host, port=port, timeout=timeout)
18+
try:
19+
connection.request("HEAD", "/")
20+
return True
21+
except Exception as e:
22+
error = e
23+
finally:
24+
connection.close()
25+
raise error
26+
27+
28+
async def site_is_online_async(url, timeout=2):
29+
"""Return True if the target URL is online.
30+
31+
Raise an exception otherwise.
32+
"""
33+
error = Exception("unknown error")
34+
parser = urlparse(url)
35+
host = parser.netloc or parser.path.split("/")[0]
36+
for scheme in ("http", "https"):
37+
target_url = scheme + "://" + host
38+
async with aiohttp.ClientSession() as session:
39+
try:
40+
await session.head(target_url, timeout=timeout)
41+
return True
42+
except asyncio.exceptions.TimeoutError:
43+
error = Exception("timed out")
44+
except Exception as e:
45+
error = e
46+
raise error
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""This module provides the CLI for RP Checker."""
2+
3+
import argparse
4+
5+
6+
def read_user_cli_args():
7+
"""Handle the CLI arguments and options"""
8+
parser = argparse.ArgumentParser(
9+
prog="rpchecker", description="check the availability of websites"
10+
)
11+
parser.add_argument(
12+
"-u",
13+
"--urls",
14+
metavar="URLs",
15+
nargs="+",
16+
type=str,
17+
default=[],
18+
help="enter one or more website URLs",
19+
)
20+
parser.add_argument(
21+
"-f",
22+
"--input-file",
23+
metavar="FILE",
24+
type=str,
25+
default="",
26+
help="read URLs from a file",
27+
)
28+
parser.add_argument(
29+
"-a",
30+
"--asynchronous",
31+
action="store_true",
32+
help="run the connectivity check asynchronously",
33+
)
34+
return parser.parse_args()
35+
36+
37+
def display_check_result(result, url, error=""):
38+
"""Display the result of a connectivity check."""
39+
print(f'The status of "{url}" is:', end=" ")
40+
if result:
41+
print('"Online!" 👍')
42+
else:
43+
print(f'"Offline?" 👎 \n Error: "{error}"')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
python.org
2+
pypi.org
3+
docs.python.org
4+
peps.python.org
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Build a Site Connectivity Checker in Python
2+
3+
RP Checker is a site connectivity checker utility. It takes one or more website URLs and checks if those sites are online. It can perform the connectivity checks either synchronously or asynchronously.
4+
5+
## Installation
6+
7+
1. Create a Python virtual environment
8+
9+
```sh
10+
$ python -m venv ./venv
11+
$ source venv/bin/activate
12+
(venv) $
13+
```
14+
15+
2. Install the requirements
16+
17+
```
18+
(venv) $ python -m pip install -r requirements.txt
19+
```
20+
21+
## Run the Project
22+
23+
```sh
24+
(venv) $ python -m rpchecker -u python.org
25+
The status of "python.org" is: "Online!" 👍
26+
```
27+
28+
## Features
29+
30+
RP Checker provides the following options:
31+
32+
- `-u` or `--urls` takes one or more URLs and checks if they're online.
33+
- `-f` or `--input-file` takes a file containing a list of URLs to check.
34+
- `-a` or `--asynchronous` runs the check asynchronously.
35+
36+
## About the Author
37+
38+
Leodanis Pozo Ramos - Email: [email protected]
39+
40+
## License
41+
42+
Distributed under the MIT license. See `LICENSE` in the root directory of this `materials` repo for more information.

0 commit comments

Comments
 (0)