欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)的Google IP 可用性檢測腳本

 更新時(shí)間:2015年04月23日 09:04:48   投稿:junjie  
這篇文章主要介紹了Python實(shí)現(xiàn)的Google IP 可用性檢測腳本,本文腳本需要Python 3.4+環(huán)境,需要的朋友可以參考下

需要 Python 3.4+,一個(gè)參數(shù)用來選擇測試搜索服務(wù)還是 GAE 服務(wù)。測試 GAE 服務(wù)的話需要先修改開頭的兩個(gè)變量。從標(biāo)準(zhǔn)輸入讀取 IP 地址或者 IP 段(形如 192.168.0.0/16)列表,每行一個(gè)??捎?IP 輸出到標(biāo)準(zhǔn)輸出。實(shí)時(shí)測試結(jié)果輸出到標(biāo)準(zhǔn)錯(cuò)誤。50 線程并發(fā)。

checkgoogleip

#!/usr/bin/env python3
 
import sys
from ipaddress import IPv4Network
import http.client as client
from concurrent.futures import ThreadPoolExecutor
import argparse
import ssl
import socket
 
# 先按自己的情況修改以下幾行
APP_ID = 'your_id_here'
APP_PATH = '/fetch.py'
 
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context.load_verify_locations('/etc/ssl/certs/ca-certificates.crt')
 
class HTTPSConnection(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._context.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)
   except 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 KeyboardInterrupt:
   raise
  except 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()

相關(guān)文章

  • Python寫的Tkinter程序屏幕居中方法

    Python寫的Tkinter程序屏幕居中方法

    這篇文章主要介紹了Python寫的Tkinter程序屏幕居中方法,Tkinter是一個(gè)python模塊,是一個(gè)調(diào)用Tcl/Tk的接口,它是一個(gè)跨平臺的腳本圖形界面接口,需要的朋友可以參考下
    2015-03-03
  • Python3.0 實(shí)現(xiàn)決策樹算法的流程

    Python3.0 實(shí)現(xiàn)決策樹算法的流程

    這篇文章主要介紹了Python3.0 實(shí)現(xiàn)決策樹算法的流程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python實(shí)現(xiàn)微信自動(dòng)回復(fù)信息的功能(根據(jù)不同信息回復(fù)對應(yīng)的信息)

    Python實(shí)現(xiàn)微信自動(dòng)回復(fù)信息的功能(根據(jù)不同信息回復(fù)對應(yīng)的信息)

    這篇文章主要介紹了Python實(shí)現(xiàn)微信自動(dòng)回復(fù)信息的功能(根據(jù)不同信息回復(fù)對應(yīng)的信息),我們使用的第三方包是UIAutomation,結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • 簡述:我為什么選擇Python而不是Matlab和R語言

    簡述:我為什么選擇Python而不是Matlab和R語言

    這篇文章主要介紹了簡述:我為什么選擇Python而不是Matlab和R語言,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • 使用django-suit為django 1.7 admin后臺添加模板

    使用django-suit為django 1.7 admin后臺添加模板

    前面我們介紹了Django-grappelli給admin添加模板,可是使用中發(fā)現(xiàn)inline有點(diǎn)問題,所以就換了今天我們要談的Django-suit,貌似要稍微好一些
    2014-11-11
  • Python中整數(shù)的緩存機(jī)制講解

    Python中整數(shù)的緩存機(jī)制講解

    今天小編就為大家分享一篇關(guān)于Python中整數(shù)的緩存機(jī)制講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 最新評論