@@ -49,7 +49,8 @@ class SimpleAsyncHTTPClient(object):
49
49
50
50
def __new__ (cls , io_loop = None , max_clients = 10 ,
51
51
max_simultaneous_connections = None ,
52
- force_instance = False ):
52
+ force_instance = False ,
53
+ hostname_mapping = None ):
53
54
"""Creates a SimpleAsyncHTTPClient.
54
55
55
56
Only a single SimpleAsyncHTTPClient instance exists per IOLoop
@@ -61,6 +62,11 @@ def __new__(cls, io_loop=None, max_clients=10,
61
62
only for compatibility with the curl-based AsyncHTTPClient. Note
62
63
that these arguments are only used when the client is first created,
63
64
and will be ignored when an existing client is reused.
65
+
66
+ hostname_mapping is a dictionary mapping hostnames to IP addresses.
67
+ It can be used to make local DNS changes when modifying system-wide
68
+ settings like /etc/hosts is not possible or desirable (e.g. in
69
+ unittests).
64
70
"""
65
71
io_loop = io_loop or IOLoop .instance ()
66
72
if io_loop in cls ._ASYNC_CLIENTS and not force_instance :
@@ -71,6 +77,7 @@ def __new__(cls, io_loop=None, max_clients=10,
71
77
instance .max_clients = max_clients
72
78
instance .queue = collections .deque ()
73
79
instance .active = {}
80
+ instance .hostname_mapping = hostname_mapping
74
81
if not force_instance :
75
82
cls ._ASYNC_CLIENTS [io_loop ] = instance
76
83
return instance
@@ -97,7 +104,7 @@ def _process_queue(self):
97
104
request , callback = self .queue .popleft ()
98
105
key = object ()
99
106
self .active [key ] = (request , callback )
100
- _HTTPConnection (self .io_loop , request ,
107
+ _HTTPConnection (self .io_loop , self , request ,
101
108
functools .partial (self ._on_fetch_complete ,
102
109
key , callback ))
103
110
@@ -111,9 +118,10 @@ def _on_fetch_complete(self, key, callback, response):
111
118
class _HTTPConnection (object ):
112
119
_SUPPORTED_METHODS = set (["GET" , "HEAD" , "POST" , "PUT" , "DELETE" ])
113
120
114
- def __init__ (self , io_loop , request , callback ):
121
+ def __init__ (self , io_loop , client , request , callback ):
115
122
self .start_time = time .time ()
116
123
self .io_loop = io_loop
124
+ self .client = client
117
125
self .request = request
118
126
self .callback = callback
119
127
self .code = None
@@ -130,6 +138,8 @@ def __init__(self, io_loop, request, callback):
130
138
else :
131
139
host = parsed .netloc
132
140
port = 443 if parsed .scheme == "https" else 80
141
+ if self .client .hostname_mapping is not None :
142
+ host = self .client .hostname_mapping .get (host , host )
133
143
134
144
if parsed .scheme == "https" :
135
145
# TODO: cert verification, etc
0 commit comments