forked from NathanPang001/PYLinkChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPYLinkChecker.py
62 lines (49 loc) · 2.03 KB
/
PYLinkChecker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#Nathan Pang
#108660184
#25/09/2020
#optional requirements: version and supporting both Windows and Unix Style command line args
import sys
import argparse
import urllib3
#import requests
http = urllib3.PoolManager()
#Checking for number of arguments with if/elif
if len(sys.argv) == 1:
print("No arguments, please enter either -file (filename), -url (link), or -v")
print("Windows and Unix style commands also work, --v, /v, etc")
elif len(sys.argv) == 3:
#File Checker
if(sys.argv[1] == "-file" or sys.argv[1] == "--file" or sys.argv[1] == "/file"):
print("File Checker")
try:
with open(sys.argv[2], 'r') as f:
lines = [line.rstrip('\n') for line in f.readlines()]
for line in lines:
if ('http://' in line or 'https://' in line):
print (line , "is a valid link")
else:
print (line, "is not a valid link")
except:
print("Sorry, file not found")
#URL Checker
elif(sys.argv[1] == "-url" or sys.argv[1] == "--url" or sys.argv[1] == "/url"):
print("URL Checker")
try:
#testing with requests
#response = requests.get(sys.argv[2])
#with urllib3
response = http.request('GET', sys.argv[2])
if response.status == 200:
print(sys.argv[2], ": good")
elif response.status == 400 or response == 404:
print(sys.argv[2], ": bad")
else:
print(sys.argv[2], ": unknown")
except:
print("Sorry, the link is broken, please fix it and try again!")
#Version Option
elif(sys.argv[1] == "-v" or sys.argv[1] == "--v" or sys.argv[1] == "/v"):
print("This is version 0.1 of the PYLinkChecker")
elif len(sys.argv) == 2:
print("Not enough entered, please enter the name of a file or a url")
print("END")