前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python测试request代理IP是否替换

python测试request代理IP是否替换

原创
作者头像
阿秋数据采集
发布2023-02-28 15:39:52
6500
发布2023-02-28 15:39:52
举报
文章被收录于专栏:HTTP教程HTTP教程

嗨,大家好!今天我们来讨论如何使用Python测试Request模块中的HTTP代理是否成功替换了我们的真实IP地址。

下面是一个使用Python测试HTTP代理的示例代码:

代码语言:python
复制
import requests

# 设置HTTP代理
proxies = {
    "http": "http://HTTP代理:端口号",
    "https": "https://HTTP代理:端口号"
}

# 发送HTTP请求
response = requests.get("http://httpbin.org/ip", proxies=proxies)

# 检查请求头中的IP地址print(response.json())

在这个示例代码中,我们首先设置了HTTP代理和端口号,然后使用Request模块发送HTTP请求。在发送请求时,我们将HTTP代理作为参数传递给了Request模块的代理参数。最后,我们检查了请求头中的IP地址,以确保我们的真实IP被HTTP代理成功替换了。

如果我们的HTTP代理设置正确,并且我们的真实IP地址被成功替换了,我们应该会在控制台中看到类似于以下内容的输出:

{"origin": "HTTP代理, 真实IP"}

以上只是测一次,如果想要更准确,可以多循环测试几次:

代码语言:python
复制
class CheckIP:
    """
    检查IP是否更换成功
    http:    http://ip.tool.chinaz.com/
    https:   https://ipip.net/ip/
    """

    @staticmethod
    def MyIP():
        """
        https://ipip.net/
        @return: 自己的 IP
        """
        url = "https://ipip.net/ip/"
        headers = {
            "Host": "www.ipip.net",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",
            "Connection": "close"
        }
        res = requests.get(url, headers=headers)
        ip_pat = '<input type="text" name="ip" value="(.*?)"'
        ip = re.findall(ip_pat, res.text)
        print(str(ip) + "\tMyIP")
        return ip

    @staticmethod
    def Http(proxies):
        """
        http://ip.tool.chinaz.com/
        @param proxies: 代理 http
        @return:        True/False
        """
        url = "http://ip.tool.chinaz.com/"
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",
            "Connection": "close"
        }
        try:
            res = requests.get(url, headers=headers, proxies=proxies)
            if res.status_code == 200:
                ip_pat = '<dd class="fz24">(.*?)</dd>'
                ip = re.findall(ip_pat, res.text)
                print(str(ip) + "\t代理成功")
            return True
        except Exception:
            print("代理失败")
            return False

    @staticmethod
    def Https(proxies):
        """
        https://ipip.net/
        @param proxies: 代理 https
        @return:        True/False
        """
        url = "https://ipip.net/ip/"
        headers = {
            "Host": "www.ipip.net",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0",
            "Connection": "close"
        }
        try:
            res = requests.get(url, headers=headers, proxies=proxies)
            if res.status_code == 200:
                ip_pat = '<input type="text" name="ip" value="(.*?)"'
                ip = re.findall(ip_pat, res.text)
                print(str(ip) + "\t代理成功")
            return True
        except Exception:
            print("代理失败")
            return False

但是需要注意的是,有些HTTP代理可能已经被封禁或失效,而有些HTTP代理可能会泄漏我们的真实IP。因此,在使用HTTP代理时,我们需要谨慎选择可靠的HTTP代理,优质的HTTP代理厂商能为我们提供更安全可靠、高质量的HTTP代理,可用性也能得到保证,无需我们反复测试其可用性。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档