OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import logging |
| 6 import time |
| 7 |
| 8 from future import Future |
| 9 from url_fetcher import UrlFetcher |
| 10 from urllib2 import urlopen, Request, HTTPError |
| 11 |
| 12 |
| 13 class UrlFetcherUrllib2(UrlFetcher): |
| 14 """UrlFetcher implementation for use outside of AppEngine. Does NOT support |
| 15 async fetching, so FetchAsync calls are still blocking. |
| 16 """ |
| 17 class _Response(object): |
| 18 def __init__(self, code, content=None, headers={}): |
| 19 self.status_code = code |
| 20 self.content = content |
| 21 self.headers = headers |
| 22 |
| 23 def FetchImpl(self, url, headers): |
| 24 request = Request(url, headers=headers) |
| 25 try: |
| 26 urlresponse = urlopen(request) |
| 27 response = self._Response(urlresponse.code, urlresponse.read(), |
| 28 urlresponse.headers.dict) |
| 29 urlresponse.close() |
| 30 return response |
| 31 except HTTPError as e: |
| 32 return self._Response(e.code, e.reason, e.headers.dict) |
| 33 |
| 34 def FetchAsyncImpl(self, url, headers): |
| 35 return Future(callback=lambda: self.FetchImpl(url, headers)) |
OLD | NEW |