requests测试代理IP是否可用

def test_ip():

    test_url = "https://www.baidu.com"

    headers = {

        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'

    }

    while True:

        temp_ip, temp_port = getIp()

        print(f"开始测试 {temp_ip}:{temp_port}")

        proxies = {

            "http": f"http://{temp_ip}:{temp_port}/",

            "https": f"http://{temp_ip}:{temp_port}/"

        }

        try:

            result = requests.get(url=test_url, headers=headers, proxies=proxies, timeout=3)

            if result.status_code == 200:

                print(f"{temp_ip}:{temp_port},可通baidu")

                print(result.text)

                return temp_ip, temp_port

            else:

                print(f"{temp_ip}:{temp_port},不通baidu")

                continue

        except Exception as exception:

            print(exception)

            continue

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 大师兄的Python学习笔记(十九): Python与(XML和JSON)师兄的Python学习笔记(二十一): ...
    superkmi阅读 2,190评论 0 4
  • 使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就...
    SkTj阅读 3,556评论 0 0
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,635评论 28 53
  • 人工智能是什么?什么是人工智能?人工智能是未来发展的必然趋势吗?以后人工智能技术真的能达到电影里机器人的智能水平吗...
    ZLLZ阅读 4,001评论 0 5
  • 首先介绍下自己的背景: 我11年左右入市到现在,也差不多有4年时间,看过一些关于股票投资的书籍,对于巴菲特等股神的...
    瞎投资阅读 5,828评论 3 8