-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (136 loc) · 4.69 KB
/
main.py
File metadata and controls
137 lines (136 loc) · 4.69 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding=utf-8 -*-
import os
import requests,threading,logging
from sys import exit
from shutil import copyfile
from tkinter import filedialog
import time
def CheckUrl(url):
try:
URLOBJ=requests.get(url,timeout=(5,10))
except Exception as e:
logging.error(str(e))
return 'Err'
a=0
if URLOBJ.status_code == 200:
logging.debug(url+' TCPing success')
return True
else:
logging.info(url+' TCPing failed,status code:'+str(URLOBJ.status_code))
return False
def urllen(text):
while len(text) < 60:
text+=' '
return text
def Print(urls,names):
file=''
try:
file = open('EXISTURLS.TXT','a',encoding='utf-8')
except Exception as e:
logging.error(str(e))
time.sleep(5)
exit(1)
for z in range(len(urls)):
resp = CheckUrl(urls[z])
if resp == True:
print('| {} | {} | {} '.format(urllen(urls[z]), 'Accessible',urllen(names[z])))
try:
file.write(urls[z]+' '+names[z]+'\r\n')
except Exception as e:
logging.error(str(e))
time.sleep(5)
exit(1)
elif resp == 'Err':
print('| {} | {} | {} '.format(urllen(urls[z]), 'ERROR',urllen(names[z])))
elif resp == False:
print('| {} | {} | {} '.format(urllen(urls[z]), 'Not-accessible',urllen(names[z])))
z+=1
try:
file.close()
except Exception as e:
logging.error(str(e))
time.sleep(5)
exit(1)
return 0
if __name__ == '__main__':
logging.basicConfig(filename='log.txt',filemode='w',level=logging.DEBUG)
m = input('Please choose url file:\n[0] PROGRAM INSTALL PATH\\URLS.TXT\n[1]User input file path')
fileName = ''
if m == '0':
fileName = '.\\URLS.TXT'
elif m == '1':
fileName = filedialog.askopenfilename(defaultextension='.txt',filetypes=[('TXT','.txt')],title='Choose URL file')
else:
print('PLEASE INPUT 0 OR 1')
time.sleep(5)
exit(1)
if fileName == '':
print('PLEASE CHOOSE A URL FILE')
time.sleep(5)
exit(1)
elif fileName[-4:] != '.txt':
print('PLEASE CHOOSE A URL FILE(TXT)')
time.sleep(5)
exit(1)
urllist = []
namelist = []
stlist = []
existurlfile = 'EXISTURLS'+str(int(time.time()))+'.TXT'
try:
with open(existurlfile,'w',encoding='utf-8') as f:
f.close()
with open(fileName,'r',encoding='utf-8-sig') as f:
data=f.readlines()
f.close()
except Exception as e:
logging.error(str(e))
time.sleep(5)
exit(1)
for i in data:
try:
urllist.append(i.split(' ')[0])
namelist.append(i.split(' ')[-1].strip('\n').strip('\r'))
except Exception as e:
logging.error('INPUT IS NOT LEGAL')
logging.error(str(e))
time.sleep(5)
exit(1)
print('-----------------------------------------')
starttime=time.time()
logging.debug('Start main part at:'+time.ctime(starttime))
try:
th1 = threading.Thread(target=Print, args=[urllist[:int(len(urllist)/3-1)],namelist[:int(len(urllist)/3-1)]])
logging.debug('Create Thread-1 object')
th2 = threading.Thread(target=Print, args=[urllist[int(len(urllist)/3):int((len(urllist)/3)*2-1)],namelist[int(len(urllist)/3):int((len(urllist)/3)*2-1)]])
logging.debug('Create Thread-2 object')
th3 = threading.Thread(target=Print, args=[urllist[int((len(urllist)/3)*2):],namelist[int((len(urllist)/3)*2):]])
logging.debug('Create Thread-3 object')
th1.start()
logging.debug('Run Thread-1 object')
th2.start()
logging.debug('Run Thread-2 object')
th3.start()
logging.debug('Run Thread-3 object')
th1.join()
th2.join()
th3.join()
except Exception as e:
logging.error(str(e))
time.sleep(5)
exit(1)
endtime=time.time()
logging.debug('Finished main part at:'+time.ctime(endtime))
logging.debug('Use time:'+str(endtime-starttime)+'s')
print('-----------------------------------------')
uin=input('Are you want to change your original file?(y,n)\r\n>>')
if uin == 'y':
try:
copyfile('EXISTURLS.TXT','text.txt')
logging.debug('Copy file...')
except Exception as e:
logging.error(str(e))
time.sleep(5)
exit(1)
else:
os.remove(existurlfile)
exit()