Skip to content

Commit 90064e2

Browse files
committed
define urlopen timeout var
1 parent 9e63dec commit 90064e2

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

core/basemaps/mapservice.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
from .. import settings
4242
USER_AGENT = settings.user_agent
4343

44+
TIMEOUT = 4
45+
4446
# Set mosaic backgroung image color, it will be the base color for area not covered
4547
# by the map service (ie when requests return non valid data)
4648
MOSAIC_BKG_COLOR = (128,128,128,255)
@@ -580,7 +582,7 @@ def downloadTile(self, laykey, col, row, zoom):
580582
try:
581583
#make request
582584
req = urllib.request.Request(url, None, self.headers)
583-
handle = urllib.request.urlopen(req, timeout=3)
585+
handle = urllib.request.urlopen(req, timeout=TIMEOUT)
584586
#open image stream
585587
data = handle.read()
586588
handle.close()

core/proj/srv.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525
import json
2626

2727
from .. import settings
28+
2829
USER_AGENT = settings.user_agent
2930

31+
DEFAULT_TIMEOUT = 2
32+
REPROJ_TIMEOUT = 60
33+
3034
######################################
3135
# EPSG.io
3236
# https://github.com/klokantech/epsg.io
@@ -39,7 +43,7 @@ def ping():
3943
url = "http://epsg.io"
4044
try:
4145
rq = Request(url, headers={'User-Agent': USER_AGENT})
42-
urlopen(rq, timeout=1)
46+
urlopen(rq, timeout=DEFAULT_TIMEOUT)
4347
return True
4448
except URLError as e:
4549
log.error('Cannot ping {} web service, {}'.format(url, e.reason))
@@ -66,7 +70,7 @@ def reprojPt(epsg1, epsg2, x1, y1):
6670

6771
try:
6872
rq = Request(url, headers={'User-Agent': USER_AGENT})
69-
response = urlopen(rq).read().decode('utf8')
73+
response = urlopen(rq, timeout=REPROJ_TIMEOUT).read().decode('utf8')
7074
except (URLError, HTTPError) as err:
7175
log.error('Http request fails url:{}, code:{}, error:{}'.format(url, err.code, err.reason))
7276
raise
@@ -110,7 +114,7 @@ def reprojPts(epsg1, epsg2, points):
110114

111115
try:
112116
rq = Request(url, headers={'User-Agent': USER_AGENT})
113-
response = urlopen(rq).read().decode('utf8')
117+
response = urlopen(rq, timeout=REPROJ_TIMEOUT).read().decode('utf8')
114118
except (URLError, HTTPError) as err:
115119
log.error('Http request fails url:{}, code:{}, error:{}'.format(url, err.code, err.reason))
116120
raise
@@ -127,7 +131,7 @@ def search(query):
127131
url = url.replace("{QUERY}", query)
128132
log.debug('Search crs : {}'.format(url))
129133
rq = Request(url, headers={'User-Agent': USER_AGENT})
130-
response = urlopen(rq).read().decode('utf8')
134+
response = urlopen(rq, timeout=DEFAULT_TIMEOUT).read().decode('utf8')
131135
obj = json.loads(response)
132136
log.debug('Search results : {}'.format([ (r['code'], r['name']) for r in obj['results'] ]))
133137
return obj['results']
@@ -138,7 +142,7 @@ def getEsriWkt(epsg):
138142
url = url.replace("{CODE}", str(epsg))
139143
log.debug(url)
140144
rq = Request(url, headers={'User-Agent': USER_AGENT})
141-
wkt = urlopen(rq).read().decode('utf8')
145+
wkt = urlopen(rq, timeout=DEFAULT_TIMEOUT).read().decode('utf8')
142146
return wkt
143147

144148

@@ -162,7 +166,7 @@ def reprojPt(epsg1, epsg2, x1, y1):
162166
url = url.replace("{CRS2}", str(epsg2))
163167

164168
rq = Request(url, headers={'User-Agent': USER_AGENT})
165-
response = urlopen(rq).read().decode('utf8')
169+
response = urlopen(rq, timeout=REPROJ_TIMEOUT).read().decode('utf8')
166170
obj = json.loads(response)
167171

168172
return (float(obj['point']['x']), float(obj['point']['y']))

operators/lib/osm/nominatim.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from urllib.request import Request
1010
from urllib.parse import quote_plus
1111

12+
TIMEOUT = 2
1213

1314
def nominatimQuery(
1415
query,
@@ -31,7 +32,7 @@ def nominatimQuery(
3132
if user_agent:
3233
req.add_header('User-Agent', user_agent)
3334

34-
response = urlopen(req)
35+
response = urlopen(req, timeout=TIMEOUT)
3536

3637
r = json.loads(response.read().decode('utf-8'))
3738

operators/lib/osm/overpy/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from urllib.request import urlopen, Request
2626
from urllib.error import HTTPError
2727

28+
TIMEOUT = 120
2829

2930
def is_valid_type(element, cls):
3031
"""
@@ -77,7 +78,7 @@ def query(self, query):
7778
req.add_header('User-Agent', self.user_agent)
7879

7980
try:
80-
f = urlopen(req, query)
81+
f = urlopen(req, query, timeout=TIMEOUT)
8182
except HTTPError as e:
8283
f = e
8384

0 commit comments

Comments
 (0)