forked from Py-Contributors/awesomeScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Website status checker (Py-Contributors#201)
* Add script to check status of web address * flake8 fix and update README.md * Flake8 fix
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## Website Status Checker | ||
|
||
This script will check the status of the web address which you will input | ||
|
||
## Installation | ||
|
||
- Install Python to your system if it's not available. | ||
|
||
**Now you are ready to run the code** | ||
|
||
## Note: | ||
|
||
- Copy the file **website_status_checker.py** and store it in any folder or Desktop. | ||
|
||
## Steps to run: | ||
|
||
- Open the **website_status_checker.py** file i.e run it. | ||
- To run it, you can open command prompt/shell/terminal in the containing folder and type the following command: | ||
``` | ||
python website_status_checker.py | ||
``` | ||
## Output: | ||
The Status Code of the Website | ||
## Contributor | ||
- **Michael Kossowsky (https://github.com/mikek4233)** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import urllib.request | ||
|
||
|
||
def check_website_status(): | ||
prompt = "Please enter a website URL: " | ||
while True: | ||
# asks for URL to check | ||
# adds in https or http if necessary | ||
url = str(input(prompt)) | ||
if url.startswith('https://'): | ||
pass | ||
elif url.startswith('http://'): | ||
pass | ||
else: | ||
url = 'https://' + url | ||
try: | ||
# tries to make a request to the URL that was input | ||
# uses defined headers that are not a "bot" | ||
headers = {} | ||
headers['User-Agent'] = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \ | ||
(KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36") | ||
req = urllib.request.Request(url, headers=headers) | ||
page = urllib.request.urlopen(req) | ||
code = str(page.getcode()) | ||
print('The website ' + url + ' has returned a ' + code + ' code') | ||
break | ||
except Exception as e: | ||
# if there is an error it will ask if you want to try again | ||
print(str(e)) | ||
print("Make sure you are entering a valid URL") | ||
try_again = input("Do you want to try again (y/n): ") | ||
try_again = try_again.lower() | ||
if try_again == 'y': | ||
continue | ||
else: | ||
break | ||
|
||
|
||
check_website_status() |