Proxy with urllib2

前端 未结 7 1553
借酒劲吻你
借酒劲吻你 2020-11-22 17:15

I open urls with:

site = urllib2.urlopen(\'http://google.com\')

And what I want to do is connect the same way with a proxy I got somewhere telli

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 17:47

    You can set proxies using environment variables.

    import os
    os.environ['http_proxy'] = '127.0.0.1'
    os.environ['https_proxy'] = '127.0.0.1'
    

    urllib2 will add proxy handlers automatically this way. You need to set proxies for different protocols separately otherwise they will fail (in terms of not going through proxy), see below.

    For example:

    proxy = urllib2.ProxyHandler({'http': '127.0.0.1'})
    opener = urllib2.build_opener(proxy)
    urllib2.install_opener(opener)
    urllib2.urlopen('http://www.google.com')
    # next line will fail (will not go through the proxy) (https)
    urllib2.urlopen('https://www.google.com')
    

    Instead

    proxy = urllib2.ProxyHandler({
        'http': '127.0.0.1',
        'https': '127.0.0.1'
    })
    opener = urllib2.build_opener(proxy)
    urllib2.install_opener(opener)
    # this way both http and https requests go through the proxy
    urllib2.urlopen('http://www.google.com')
    urllib2.urlopen('https://www.google.com')
    

提交回复
热议问题