Skip to content

Commit

Permalink
Use requests.
Browse files Browse the repository at this point in the history
requests (using sessions):
$ time ./tc-copy --bounding-pyramid 4/0/0:0/12/12 tiles.openstreetmap_org null://

real 0m4.963s
user 0m0.324s
sys  0m0.056s

urllib2:
$ time ./tc-copy --bounding-pyramid 4/0/0:0/12/12 tiles.openstreetmap_org null://

real 0m9.012s
user 0m0.192s
sys  0m0.052s
  • Loading branch information
fredj committed Oct 8, 2012
1 parent 829959f commit a254cc4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ install them with ``pip`` in a ``virtualenv``:

$ virtualenv .
$ . bin/activate
$ pip install bottle pyproj
$ pip install requests bottle pyproj

For a quick demo, run

Expand Down
25 changes: 10 additions & 15 deletions tilecloud/store/url.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# FIXME: error handling (tile.error)
import logging
from urllib2 import HTTPError, Request, urlopen

import requests
from tilecloud import TileStore


Expand All @@ -12,7 +12,7 @@ class URLTileStore(TileStore):
def __init__(self, tilelayouts, headers=None, **kwargs):
TileStore.__init__(self, **kwargs)
self.tilelayouts = tuple(tilelayouts)
self.headers = headers or {}
self.session = requests.session(headers=headers)

def get_one(self, tile):
if self.bounding_pyramid is not None:
Expand All @@ -21,16 +21,11 @@ def get_one(self, tile):
tilelayout = self.tilelayouts[hash(tile.tilecoord) %
len(self.tilelayouts)]
url = tilelayout.filename(tile.tilecoord)
request = Request(url, headers=self.headers)
try:
logger.debug('GET %s' % (url,))
response = urlopen(request)
info = response.info()
if 'Content-Encoding' in info:
tile.content_encoding = info['Content-Encoding']
if 'Content-Type' in info:
tile.content_type = info['Content-Type']
tile.data = response.read()
except HTTPError as exc:
tile.error = exc

logger.debug('GET %s' % (url,))
response = self.session.get(url)

tile.content_encoding = response.headers.get('Content-Encoding')
tile.content_type = response.headers.get('Content-Type')
tile.data = response.content
return tile

0 comments on commit a254cc4

Please sign in to comment.