検索

需要 Python 3.4+,一个参数用来选择测试搜索服务还是 GAE 服务。测试 GAE 服务的话需要先修改开头的两个变量。从标准输入读取 IP 地址或者 IP 段(形如 192.168.0.0/16)列表,每行一个。可用 IP 输出到标准输出。实时测试结果输出到标准错误。50 线程并发。

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()

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Pythonの融合リスト:適切な方法を選択しますPythonの融合リスト:適切な方法を選択しますMay 14, 2025 am 12:11 AM

Tomergelistsinpython、あなたはオペレーター、extendmethod、listcomfulting、olitertools.chain、それぞれの特異的advantages:1)operatorissimplebutlessforlargelist;

Python 3の2つのリストを連結する方法は?Python 3の2つのリストを連結する方法は?May 14, 2025 am 12:09 AM

Python 3では、2つのリストをさまざまな方法で接続できます。1)小さなリストに適したオペレーターを使用しますが、大きなリストには非効率的です。 2)メモリ効率が高い大規模なリストに適した拡張方法を使用しますが、元のリストは変更されます。 3)元のリストを変更せずに、複数のリストをマージするのに適した *オペレーターを使用します。 4)Itertools.chainを使用します。これは、メモリ効率が高い大きなデータセットに適しています。

Python Concatenateリスト文字列Python Concatenateリスト文字列May 14, 2025 am 12:08 AM

Join()メソッドを使用することは、Pythonのリストから文字列を接続する最も効率的な方法です。 1)join()メソッドを使用して、効率的で読みやすくなります。 2)サイクルは、大きなリストに演算子を非効率的に使用します。 3)リスト理解とJoin()の組み合わせは、変換が必要なシナリオに適しています。 4)redoce()メソッドは、他のタイプの削減に適していますが、文字列の連結には非効率的です。完全な文は終了します。

Pythonの実行、それは何ですか?Pythonの実行、それは何ですか?May 14, 2025 am 12:06 AM

pythonexexecutionistheprocessoftransforningpythoncodeintoexecutabletructions.1)interpreterreadSthecode、変換intobytecode、thepythonvirtualmachine(pvm)executes.2)theglobalinterpreeterlock(gil)管理委員会、

Python:重要な機能は何ですかPython:重要な機能は何ですかMay 14, 2025 am 12:02 AM

Pythonの主な機能には次のものがあります。1。構文は簡潔で理解しやすく、初心者に適しています。 2。動的タイプシステム、開発速度の向上。 3。複数のタスクをサポートするリッチ標準ライブラリ。 4.強力なコミュニティとエコシステム、広範なサポートを提供する。 5。スクリプトと迅速なプロトタイピングに適した解釈。 6.さまざまなプログラミングスタイルに適したマルチパラダイムサポート。

Python:コンパイラまたはインタープリター?Python:コンパイラまたはインタープリター?May 13, 2025 am 12:10 AM

Pythonは解釈された言語ですが、コンパイルプロセスも含まれています。 1)Pythonコードは最初にBytecodeにコンパイルされます。 2)ByteCodeは、Python Virtual Machineによって解釈および実行されます。 3)このハイブリッドメカニズムにより、Pythonは柔軟で効率的になりますが、完全にコンパイルされた言語ほど高速ではありません。

ループvs whileループ用のpython:いつ使用するか?ループvs whileループ用のpython:いつ使用するか?May 13, 2025 am 12:07 AM

useaforloopwhenteratingoverasequenceor foraspificnumberoftimes; useawhileloopwhentinuninguntinuntilaConditionismet.forloopsareidealforknownownownownownownoptinuptinuptinuptinuptinutionsituations whileoopsuitsituations withinterminedationations。

Pythonループ:最も一般的なエラーPythonループ:最も一般的なエラーMay 13, 2025 am 12:07 AM

pythonloopscanleadtoErrorslikeinfiniteloops、ModifiningListsDuringiteration、Off-Oneerrors、Zero-dexingissues、およびNestededLoopinefficiencies.toavoidhese:1)use'i

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!