【问题标题】:How can i get http requests in python 3 and have the program sort the status codes如何在 python 3 中获取 http 请求并让程序对状态代码进行排序
【发布时间】:2021-07-28 11:27:07
【问题描述】:

我有一个 cidr 符号列表,我有一个 python 程序,它从 cidr 符号生成一个 ip 地址列表。这是程序

from netaddr import IPNetwork

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

    print ('%s' % ip)

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

【问题讨论】:

  • 你有什么问题?到目前为止,您尝试过什么?

标签: python python-3.x networking http-headers http-status-codes


【解决方案1】:

试试这个:

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!')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    相关资源
    最近更新 更多