OLD | NEW |
---|---|
1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ | 5 """ |
6 A http client with support for https connections with certificate verification. | 6 A http client with support for https connections with certificate verification. |
7 | 7 |
8 The verification is based on http://tools.ietf.org/html/rfc6125#section-6.4.3 | 8 The verification is based on http://tools.ietf.org/html/rfc6125#section-6.4.3 |
9 and the code is from Lib/ssl.py in python3: | 9 and the code is from Lib/ssl.py in python3: |
10 http://hg.python.org/cpython/file/4dac45f88d45/Lib/ssl.py | 10 http://hg.python.org/cpython/file/4dac45f88d45/Lib/ssl.py |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 # to a man in the middle. | 198 # to a man in the middle. |
199 handlers.append(HTTPSHandler()) | 199 handlers.append(HTTPSHandler()) |
200 | 200 |
201 | 201 |
202 cookie_file = os.environ.get('COOKIE_FILE') | 202 cookie_file = os.environ.get('COOKIE_FILE') |
203 if cookie_file and os.path.exists(cookie_file): | 203 if cookie_file and os.path.exists(cookie_file): |
204 handlers.append( | 204 handlers.append( |
205 urllib2.HTTPCookieProcessor(cookielib.MozillaCookieJar(cookie_file))) | 205 urllib2.HTTPCookieProcessor(cookielib.MozillaCookieJar(cookie_file))) |
206 | 206 |
207 url_opener = urllib2.build_opener(*handlers) | 207 url_opener = urllib2.build_opener(*handlers) |
208 if timeout is not None: | 208 |
209 response = url_opener.open(url, timeout=timeout) | 209 status_code = None |
210 else: | 210 content = None |
211 response = url_opener.open(url) | 211 |
212 return response.code, response.read() | 212 try: |
213 if timeout is not None: | |
aarya
2014/08/26 19:01:23
None timeout is just ignored, i dont think you nee
stgao
2014/08/26 20:30:47
Nice. That makes the code cleaner.
| |
214 response = url_opener.open(url, timeout=timeout) | |
215 else: | |
216 response = url_opener.open(url) | |
217 | |
218 status_code = response.code | |
219 content = response.read() | |
220 except urllib2.HTTPError as e: | |
221 status_code = e.code | |
222 content = None | |
223 | |
224 return status_code, content | |
213 | 225 |
214 | 226 |
215 class HttpClientLocal(http_client.HttpClient): | 227 class HttpClientLocal(http_client.HttpClient): |
216 """This http client is used locally in a workstation, GCE VMs, etc.""" | 228 """This http client is used locally in a workstation, GCE VMs, etc.""" |
217 | 229 |
218 @staticmethod | 230 @staticmethod |
219 def Get(url, params={}, timeout=None): | 231 def Get(url, params={}, timeout=None): |
220 if params: | 232 if params: |
221 url = '%s?%s' % (url, urllib.urlencode(params)) | 233 url = '%s?%s' % (url, urllib.urlencode(params)) |
222 return _SendRequest(url, timeout=timeout) | 234 return _SendRequest(url, timeout=timeout) |
OLD | NEW |