【问题标题】:Username/password proxies not working correctly python requests用户名/密码代理无法正常工作 python 请求
【发布时间】:2020-01-21 22:05:10
【问题描述】:

我最近一直在尝试将代理与 Python 请求一起使用,但似乎无法让它们工作。尽管请求通过代理进行,但我所做的测试使我相信代理没有应用于请求。即使有明显不好的代理,我的请求仍然通过,这让我觉得代理根本没有被使用。为了证明这一点,我制作了一个简单的脚本(工作代理已针对本文进行了编辑):

import requests

proxy1 = {"http":"http://this:should@not:work"}
proxy2= {"http":"http://this:proxy@is.working.com:33128"}

r1 = requests.get("https://google.com", proxies=proxy2)
print(r1.status_code)
#prints 200 as expected
r2 = requests.get("https://google.com", proxies=proxy1)
print(r2.status_code)
#prints 200 which is weird since I was expecting the request to not go through

有谁知道为什么会发生这种情况以及这些请求是否真的与代理一起使用?

【问题讨论】:

    标签: python proxy python-requests


    【解决方案1】:

    在这两个示例中,您只为http 定义代理

    proxy1 = {"http": "http://this:should@not:work"}
    
    proxy2 = {"http": "http://this:proxy@is.working.com:33128"}
    

    但是你使用带有https:的url

    https://google.com
    

    所以requests 不使用代理。

    你必须为https定义代理

    proxy1 = {"https": "http://this:should@not:work"}
    
    proxy2 = {"https": "http://this:proxy@is.working.com:33128"}
    

    文档:requests: proxy


    编辑:

    使用https://httpbin.org/get,您可以测试 GET 请求,它会将您的所有标头和 IP 发回给您

    我从一个带有免费代理的页面中获取了代理,因此它可能在一段时间内无法使用

    import requests
    
    proxy = {"https": "104.244.75.26:8080"}
    
    r = requests.get("https://httpbin.org/get", proxies=proxy1)
    print(r.status_code)
    print(r.text)
    

    结果显示代理的IP

    200
    {
      "args": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.22.0"
      }, 
      "origin": "104.244.75.26, 104.244.75.26", 
      "url": "https://httpbin.org/get"
    }
    

    【讨论】:

    • 谢谢,试试这个
    猜你喜欢
    • 1970-01-01
    • 2022-12-23
    • 2018-11-05
    • 1970-01-01
    • 2022-07-01
    • 2014-11-03
    • 2014-04-21
    • 1970-01-01
    • 2013-04-02
    相关资源
    最近更新 更多