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.
8 from future
import Future
9 from url_fetcher
import UrlFetcher
10 from urllib2
import urlopen
, Request
, HTTPError
13 class UrlFetcherUrllib2(UrlFetcher
):
14 """UrlFetcher implementation for use outside of AppEngine. Does NOT support
15 async fetching, so FetchAsync calls are still blocking.
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
23 def FetchImpl(self
, url
, headers
):
24 request
= Request(url
, headers
=headers
)
26 urlresponse
= urlopen(request
)
27 response
= self
._Response
(urlresponse
.code
, urlresponse
.read(),
28 urlresponse
.headers
.dict)
31 except HTTPError
as e
:
32 return self
._Response
(e
.code
, e
.reason
, e
.headers
.dict)
34 def FetchAsyncImpl(self
, url
, headers
):
35 return Future(callback
=lambda: self
.FetchImpl(url
, headers
))