kennethreitz/requests — pull request #2049 by codedstructure (2014-05-17 22:42:52+00:00)
Title: Separated out proxy_manager_for to fix #2048

Basically identical to @sigmavirus24's suggested code - thanks for that pointer. Just added docstring.

There aren't tests for particular Adapter implementations, and I've not added any at this stage.
I have tested it with the following (which achieves what I want):

class SourceAddressAdapter(requests.adapters.HTTPAdapter):
"""
A Requests HTTP adapter to add source address specification for proxies
"""
def __init__(self, *o, **k):
self.source_address = k.pop('source_address', None)
super(SourceAddressAdapter, self).__init__(*o, **k)

def proxy_manager_for(self, proxy):
conn_params = {}
if self.source_address is not None:
conn_params['source_address'] = (self.source_address, 0)

if not proxy in self.proxy_manager:
proxy_headers = self.proxy_headers(proxy)
self.proxy_manager[proxy] = requests.adapters.proxy_from_url(
proxy,
proxy_headers=proxy_headers,
num_pools=self._pool_connections,
maxsize=self._pool_maxsize,
block=self._pool_block,
**conn_params)

return self.proxy_manager[proxy]