Python實(shí)現(xiàn)的Google IP 可用性檢測(cè)腳本
需要 Python 3.4+,一個(gè)參數(shù)用來(lái)選擇測(cè)試搜索服務(wù)還是 GAE 服務(wù)。測(cè)試 GAE 服務(wù)的話需要先修改開頭的兩個(gè)變量。從標(biāo)準(zhǔn)輸入讀取 IP 地址或者 IP 段(形如 192.168.0.0/16)列表,每行一個(gè)??捎?IP 輸出到標(biāo)準(zhǔn)輸出。實(shí)時(shí)測(cè)試結(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發(fā)送form-data請(qǐng)求及拼接form-data內(nèi)容的方法
這篇文章主要介紹了Python發(fā)送form-data請(qǐng)求及拼接form-data內(nèi)容的方法,文中采用的是requests的方式發(fā)送multipart/form-data請(qǐng)求,需要的朋友可以參考下2016-03-03
django的ORM操作 刪除和編輯實(shí)現(xiàn)詳解
這篇文章主要介紹了django的ORM操作 刪除和編輯實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
python基礎(chǔ)詳解之if循環(huán)語(yǔ)句
這篇文章主要介紹了python基礎(chǔ)詳解之if循環(huán)語(yǔ)句,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助需要的朋友可以參考下2021-04-04
Python求出0~100以內(nèi)的所有素?cái)?shù)
質(zhì)數(shù)又稱素?cái)?shù)。一個(gè)大于1的自然數(shù),除了1和它自身外,不能被其他自然數(shù)整除的數(shù)叫做質(zhì)數(shù);否則稱為合數(shù)。下面小編給大家?guī)?lái)了Python求出0~100以內(nèi)的所有素?cái)?shù)實(shí)例代碼,需要的朋友參考下2018-01-01
python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例
這篇文章主要介紹了python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Python3以GitHub為例來(lái)實(shí)現(xiàn)模擬登錄和爬取的實(shí)例講解
在本篇內(nèi)容里小編給大家分享的是關(guān)于Python3以GitHub為例來(lái)實(shí)現(xiàn)模擬登錄和爬取的實(shí)例講解,需要的朋友們可以參考下。2020-07-07
Python利用imshow制作自定義漸變填充柱狀圖(colorbar)
這篇文章主要介紹了Python利用imshow制作自定義漸變填充柱狀圖(colorbar),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

