【问题标题】:Use default proxysettings with httplib使用 httplib 的默认代理设置
【发布时间】:2014-03-31 08:01:21
【问题描述】:

有没有办法使用系统的默认代理来设置带有httplib(2)的httptunnel(无需提供信息和凭据)?

我查看了urllib2.ProxyHandler,这似乎可以满足我的所有需求。但如果我在谷歌上搜索 httplib 替代方案,我总是必须提供服务器和端口。

class urllib2.ProxyHandler([proxies])

Cause requests to go through a proxy. If proxies is given, it must be a dictionary mapping protocol names to URLs of proxies. 
The default is to read the list of proxies from the environment variables <protocol>_proxy. 
If no proxy environment variables are set, then in a Windows environment proxy settings are obtained from the registry’s Internet Settings section, and in a Mac OS X environment proxy information is retrieved from the OS X System Configuration Framework. 
To disable autodetected proxy pass an empty dictionary.`

【问题讨论】:

    标签: python proxy tunnel httplib


    【解决方案1】:

    显然 httplib 和 urllib2 在某种程度上可以互换

    remote_addr = {"host": host, "port": port}
    
    proxy = urllib2.ProxyHandler()
    proxy_opener = urllib2.build_opener(proxy)
    urllib2.install_opener(proxy_opener)
    
    http_opener = urllib2.build_opener(urllib2.HTTPHandler)
    params = urllib.urlencode({"data": 'some data'})
    url = "http://{host}:{port}".format(host=remote_addr['host'], port=remote_addr['port'])
    headers = {"Content-Type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    request = urllib2.Request(url, params, headers)
    request.get_method = lambda: 'PUT'
    url = http_opener.open(request)   
    

    【讨论】:

      猜你喜欢
      • 2010-10-12
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      相关资源
      最近更新 更多