Use the Python script to detect problems and solutions for batch website survival.
During the penetration test, there was a large project with hundreds of websites. In this way, you must first determine which websites are normal and which websites are abnormal. Therefore, I compiled a small script for future convenience.
The specific implementation code is as follows:
#! /Usr/bin/python #-*-coding: UTF-8-*-''' @ Author: joy_nick @ blog: http://byd.drow.c.xyz/''import requestsimport sysf = 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) does t requests. requestException as e: url_result_failed.append (url [I]) continueurl_result_success.append (url [I]) f. close () result_len = len (url_result_success) for I in range (0, result_len): print 'URL % s' % url_result_success [I]. strip () + 'open successfully'
The test results are as follows:
Problems:
At the beginning of the test, if the error cannot be found or does not exist, an error is reported to stop the program. Later, it was found that the response. status_code! = 200 An error occurred while retrieving the status code.
Because some websites cannot be opened, no status code is returned. So the program does not know! = 200.
Solution:
Catch exceptions using try failed t else
The Code is as follows:
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
The above is a small series of problems and solutions for implementing batch website survival Detection Using Python scripts. I hope to help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!