Using Python script to achieve batch website survival detection encountered problems and solutions

  • 2020-05-12 02:49:47
  • OfStack

When you do penetration testing, you have a big project with hundreds of websites, so you have to first determine which ones are working and which ones are not. So I made up a small script, for the convenience of use in the future.

The specific implementation code is as follows:


#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
@Author : joy_nick
@ Blog: http://byd.dropsec.xyz/
'''
import requests
import sys
f = open('url.txt', 'r')
url = f.readlines()
length = len(url)
url_result_success=[]
url_result_failed=[]
for i in range(0,length):
try:
response = requests.get(url[i].strip(), verify=False, allow_redirects=True, timeout=5)
if response.status_code != 200:
raise requests.RequestException(u"Status code error: {}".format(response.status_code))
except requests.RequestException as e:
url_result_failed.append(url[i])
continue
url_result_success.append(url[i])
f.close()
result_len = len(url_result_success)
for i in range(0,result_len):
print ' The url %s' % url_result_success[i].strip()+' Open the success '

The test results are as follows:

Problems encountered:

At the beginning of the test, as long as the encounter is not error, or does not exist, directly error stop the program. It turned out to be response.status_code! Is equal to 200 and there was an error fetching the status code.

Because some websites cannot be opened, the status code will not be returned. So the program doesn't know! Theta is equal to two hundred.

Solutions:

Catch exceptions using try except else

The specific code is:


try:
response = requests.get(url[i].strip(), verify=False, allow_redirects=True, timeout=5)
if response.status_code != 200:
raise requests.RequestException(u"Status code error: {}".format(response.status_code))
except requests.RequestException as e:
url_result_failed.append(url[i])
continue

Related articles: