Use the Python script to detect problems and solutions for batch website survival.

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.