#import sys, urllib.request
import sys, urllib.request

# now open connection to the URL:
web = sys.argv[1]
resp = urllib.request.urlopen(web)
#resp.read() will be a (huge!) string 
#with the full (HTML) of the web page
#Since it is a string we can use find() method
#in any case sys.argv[] has to be converted
#from string to BYTE object 

def fixURL(s1,s2):
	if (s2.find('http') == -1):
		print(len(s1))
		print(len(s2))
#		if (s1[-1] == '/' and s2[0] == '/'):
#			return s1+s2
#		else:
		return s1+s2
	else:
		return s2
def findURL(s1):
	i = s1.find('"'.encode())
	j = s1.find('"'.encode(),i+1)
	return s1[i+1:j]

def recWEBlist(i,l):
	if (i == 0):
		return 0
	else:
		for j in l:
			k = recWEBlist(i-1,link(j))
	return 0
	
urlList=[]
length = 100
search = 'href'.encode()
urlString = resp.read()
ind1 = urlString.find(search)
while (ind1 != -1):
	web1 = urlString[ind1:ind1+length]
#	print(web1)
	url1 = findURL(web1)
#	print(url1)
#	print(fixURL(web,url1.decode()))
	urlList = urlList + [fixURL(web,url1.decode())]
	ind1 = urlString.find(search,ind1+2)
#if sys.argv[2].encode() in s1:
#	print('found')
#else:
#	print('not found')
#print(s1[:100])
for k in urlList:
	print(k)
	try:
	    r = urllib.request.urlopen(k)
	    print('\033[31mOK'+'\n')
	except Exception as e:
	    print(str(e)+'\n')
