1. 1. 生活中的懒人 在测试网站运行状态的时候,为了免去一些重复的不必要操作,所以想着能不能用Python脚本来代劳,答案当然是肯定的,好了来说下主要思路吧! Github 用到的Python库 requests、ulib32 Run Enveronment python 36 模块 scan_port123456789101112131415161718192021222324252627282930313233343536# coding: utf-8import socketfrom multiprocessing.dummy import Pool as ThreadPooldef main(): global host_ip host = input("Enter a host to scan:") exit_port = int(input("Enter exit_port:")) processes_num = int(input("Enter processes num:")) host_ip = socket.gethostbyname(host) ports = [] print ('-' * 60) print ('Please wait, scanning host ', host_ip) print ('-' * 60) socket.setdefaulttimeout(0.5) for i in range(1,exit_port): ports.append(i) pool = ThreadPool(processes = processes_num) results = pool.map(scan_port,ports) pool.close() pool.join() print ('port scan end')def scan_port(port): try: s = socket.socket(2,1) res = s.connect_ex((host_ip,port)) if res == 0: print ('Port {}: OPEN'.format(port)) s.close() except Exception as e: print (str(e.message))if __name__ == '__main__': main() scan_content12345678910111213141516171819202122232425262728293031323334353637383940414243import requestsfrom multiprocessing.dummy import Pool as ThreadPooldef scan_content(url): r = requests.get(url) print(str(r.status_code)+' '+url+'\n')def read_file(file,url): global content content = [] for line in file: tl = line.rstrip('\n') content.append(url+tl) del content[-0] return contentdef main(): url = input("Please enter url:") print ('-' * 60) try: file = open ('..//map//func//dir.txt','r',encoding='utf-8') #file = open ('dir.txt',encoding='utf-8') except IOError: print ('open file(dir.txt) fail!') else: if (url.strip()==''): url = 'https://www.baidu.com' read_file(file,url) pool = ThreadPool(2) pool.map(scan_content,content) pool.close() pool.join() #scan_content(content) else: read_file(file,url) pool = ThreadPool(2) pool.map(scan_content,content) pool.close() pool.join() #scan_content(content) print('scan end')if __name__ == '__main__': main() main()1234567891011121314151617181920212223from func import scan_contentfrom func import scan_portsif __name__ == '__main__': print ('welcome to the scanner of xu') print ('*' * 60+'\n1.scan for content') print ('2.scan for ports') while(1): temp = input('*' * 60+'\nenter the function num: ') print ('*' * 60) if (temp=='1'): scan_content.main() continue elif (temp=='2'): scan_ports.main() print('ports') continue elif (temp=='exit'): exit() else: print('Enter Error!') continue 测试效果 哈哈,这下省事多了,还是python大法好啊!这里以百度为栗子:
  2. 2. 用到的Python库
  3. 3. Run Enveronment
  4. 4. 模块
    1. 4.1. scan_port
    2. 4.2. scan_content
    3. 4.3. main()
  5. 5. 测试效果

PortScanV1.0

生活中的懒人

在测试网站运行状态的时候,为了免去一些重复的不必要操作,所以想着能不能用Python脚本来代劳,答案当然是肯定的,好了来说下主要思路吧!

Github

用到的Python库

requestsulib32

Run Enveronment

python 36

模块

scan_port


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
# coding: utf-8
import socket
from multiprocessing.dummy import Pool as ThreadPool
def main():
global host_ip
host = input("Enter a host to scan:")
exit_port = int(input("Enter exit_port:"))
processes_num = int(input("Enter processes num:"))
host_ip = socket.gethostbyname(host)
ports = []
print ('-' * 60)
print ('Please wait, scanning host ', host_ip)
print ('-' * 60)
socket.setdefaulttimeout(0.5)
for i in range(1,exit_port):
ports.append(i)
pool = ThreadPool(processes = processes_num)
results = pool.map(scan_port,ports)
pool.close()
pool.join()
print ('port scan end')
def scan_port(port):
try:
s = socket.socket(2,1)
res = s.connect_ex((host_ip,port))
if res == 0:
print ('Port {}: OPEN'.format(port))
s.close()
except Exception as e:
print (str(e.message))
if __name__ == '__main__':
main()

scan_content


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
import requests
from multiprocessing.dummy import Pool as ThreadPool
def scan_content(url):
r = requests.get(url)
print(str(r.status_code)+' '+url+'\n')
def read_file(file,url):
global content
content = []
for line in file:
tl = line.rstrip('\n')
content.append(url+tl)
del content[-0]
return content
def main():
url = input("Please enter url:")
print ('-' * 60)
try:
file = open ('..//map//func//dir.txt','r',encoding='utf-8')
#file = open ('dir.txt',encoding='utf-8')
except IOError:
print ('open file(dir.txt) fail!')
else:
if (url.strip()==''):
url = 'https://www.baidu.com'
read_file(file,url)
pool = ThreadPool(2)
pool.map(scan_content,content)
pool.close()
pool.join()
#scan_content(content)
else:
read_file(file,url)
pool = ThreadPool(2)
pool.map(scan_content,content)
pool.close()
pool.join()
#scan_content(content)
print('scan end')
if __name__ == '__main__':
main()

main()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from func import scan_content
from func import scan_ports
if __name__ == '__main__':
print ('welcome to the scanner of xu')
print ('*' * 60+'\n1.scan for content')
print ('2.scan for ports')
while(1):
temp = input('*' * 60+'\nenter the function num: ')
print ('*' * 60)
if (temp=='1'):
scan_content.main()
continue
elif (temp=='2'):
scan_ports.main()
print('ports')
continue
elif (temp=='exit'):
exit()
else:
print('Enter Error!')
continue

测试效果

哈哈,这下省事多了,还是python大法好啊!这里以百度为栗子:

ScanResultExam