我如何在 python 3 中获取 http 请求并让程序对状态代码进行排序

How can i get http requests in python 3 and have the program sort the status codes

我有一个 cidr 符号列表,我有一个 python 从 cidr 生成 ip 地址列表的程序 notation.Here 是程序

from netaddr import IPNetwork

for ip in IPNetwork('104.24.0.0/6'):

    print ('%s' % ip)

但我想让程序循环遍历每个 ip 地址并发送 http 请求,然后搜索一些特定的状态代码,如果它找到所需的状态代码,它应该 return 哪个 ip 地址有那个状态码

试试这个:

import requests
from netaddr import IPNetwork

required_status_code = 200  # your required status code goes here

for ip in IPNetwork('104.24.0.0/6'):
    resp = requests.get(f'http://{ip}')
    if resp.status_code == requires_status_code:
        print (f'ip {ip} matches required status code!')