HTTPS proxies not working with Python's requests module

前端 未结 1 381
别跟我提以往
别跟我提以往 2021-02-06 05:21

I\'m pretty new to Python and I\'ve been using their requests module as a substitute for PHP\'s cURL library. My code is as follows

import requests
import jso         


        
相关标签:
1条回答
  • 2021-02-06 05:50

    Edit June 2019: This reply is not relevant anymore. Issue(s) are fixed.

    Edit 2: "note that even for https proxy, the proxy address' scheme is http, it's because the client and proxy server initiate the tunnelling(the CONNECT method) in plain http. However, that's may not be true 3 years ago." - From the comments

    HTTPS is 'bugged' in requests. I don't know the specifics but you can find a few other topics on this website concerning the issue. Also a Github issue is still active here. I'm suspecting you're having the problems mentioned there. If I am totally wrong, someone correct me.

    To verify:

    $~ curl --proxy https://27.254.52.99:8080 icanhazip.com
    27.254.52.99
    

    Works, but then in Python:

    >>> proxies={'https': 'https://27.254.52.99:8080'}
    >>> r = requests.get('http://icanhazip.com', headers={'User-Agent': 'Bla'}, proxies=proxies)
    print r.content
    <my ipv6 address comes up>
    

    As you can see, my address comes up which means the proxy did nothing.

    I don't understand why you are receiving a stacktrace. Maybe because your API is on HTTPS as well (?). Or maybe your API is just... down.

    Anyway, the proxy does work in requests if its over HTTP.

    >>> proxies={'http': 'http://27.254.52.99:8080'}
    >>> r = requests.head('http://icanhazip.com', headers={'User-Agent': 'Bla'}, proxies=proxies)
    print r.content
    27.254.52.99
    
    0 讨论(0)
提交回复
热议问题