Google IP availability detection script implemented by Python

Source: Internet
Author: User
This article mainly introduces the GoogleIP availability detection script implemented by Python. this script requires the Python3.4 + environment. For more information, see Python 3.4 +, A parameter is used to test the search service or GAE service. To test the GAE service, you must first modify the two variables at the beginning. Read the list of IP addresses or IP segments (such as 192.168.0.0/16) from the standard input. each line contains one. The available IP address is output to the standard output. Real-time test results are output to standard errors. 50 concurrent threads.

Checkgoogleip

#! /Usr/bin/env python3 import sysfrom ipaddress import IPv4Networkimport http. client as clientfrom concurrent. futures import ThreadPoolExecutorimport argparseimport sslimport socket # First modify the following lines according to your own situation: APP_ID = 'Your _ id_here 'APP _ PATH ='/fetch. py 'context = ssl. SSLContext (ssl. PROTOCOL_TLSv1) context. verify_mode = ssl. CERT_REQUIREDcontext.load_verify_locations ('/etc/ssl/certs/ca-certificates.crt') class HTT PSConnection (client. HTTPSConnection): def _ init _ (self, * args, hostname = None, ** kwargs): self. _ hostname = hostname super (). _ init _ (* args, ** kwargs) def connect (self): super (client. HTTPSConnection, self ). connect () if self. _ tunnel_host: server_hostname = self. _ tunnel_host else: server_hostname = self. _ hostname or self. host sni_hostname = server_hostname if ssl. HAS_SNI else None self. sock = self. _ c Ontext. wrap_socket (self. sock, server_hostname = sni_hostname) if not self. _ context. check_hostname and self. _ check_hostname: try: ssl. match_hostname (self. sock. getpeercert (), server_hostname) failed t Exception: self. sock. shutdown (socket. SHUT_RDWR) self. sock. close () raise def check_ip_p (ip, func): if func (ip): print (ip, flush = True) def check_for_gae (ip ): return _ check (APP_ID + '.appspot.com', APP_PATH, Ip) def check_for_search (ip): return _ check ('www .google.com ','/', ip) def _ check (host, path, ip ): for chance in range (1,-1,-1): try: conn = HTTPSConnection (ip, timeout = 5, context = context, hostname = host,) conn. request ('GET', path, headers = {'host': Host,}) response = conn. getresponse () if response. status <400: print ('good: ', ip, file = sys. stderr) else: raise Exception ('http Error % s % s' % (Response. status, response. reason) return True except T KeyboardInterrupt: raise except T Exception as e: if isinstance (e, ssl. certificateError): print ('WARN: % s is not Google \'s! '% Ip, file = sys. stderr) chance = 0 if chance = 0: print ('Bad: ', ip, e, file = sys. stderr) return False else: print ('Re: ', ip, e, file = sys. stderr) def main (): parser = argparse. argumentParser (description = 'check Google IPs') parser. add_argument ('service', choices = ['search', 'gae'], help = 'service to check') args = parser. parse_args () func = globals () ['Check _ for _ '+ args. service] count = 0 with ThreadPoolExecutor (max_workers = 50) as executor: for l in sys. stdin: l = l. strip () if '/' in l: for ip in IPv4Network (l ). hosts (): executor. submit (check_ip_p, str (ip), func) count + = 1 else: executor. submit (check_ip_p, l, func) count + = 1 print ('% d IP checked. '% count) if _ name _ =' _ main _ ': main ()

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.