Skip to content

Commit b983494

Browse files
committed
pyosmium tools: set timeouts via requests
1 parent 055254c commit b983494

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/osmium/replication/server.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ class ReplicationServer:
3636
internally keeps a connection to the server making downloads faster.
3737
"""
3838

39-
def __init__(self, url: str, diff_type: str = 'osc.gz') -> None:
39+
def __init__(self, url: str, diff_type: str = 'osc.gz',
40+
extra_request_params: Optional[Mapping[str, Any]] = None) -> None:
4041
self.baseurl = url
4142
self.diff_type = diff_type
43+
if extra_request_params is None:
44+
self.extra_request_params = dict(timeout=60, stream=True)
45+
else:
46+
self.extra_request_params = extra_request_params
4247
self.session: Optional[requests.Session] = None
4348

4449
def close(self) -> None:
@@ -77,17 +82,19 @@ def open_url(self, url: urlrequest.Request) -> Any:
7782
svr = ReplicationServer()
7883
svr.open_url = opener.open
7984
"""
80-
headers = dict()
81-
for h in url.header_items():
82-
headers[h[0]] = h[1]
85+
if 'headers' in self.extra_request_params:
86+
get_params = self.extra_request_params
87+
else:
88+
get_params = dict(self.extra_request_params)
89+
get_params['headers'] = {k: v for k,v in url.header_items()}
8390

8491
if self.session is not None:
85-
return self.session.get(url.get_full_url(), headers=headers, stream=True)
92+
return self.session.get(url.get_full_url(), **get_params)
8693

8794
@contextmanager
8895
def _get_url_with_session() -> Iterator[requests.Response]:
8996
with requests.Session() as session:
90-
request = session.get(url.get_full_url(), headers=headers, stream=True)
97+
request = session.get(url.get_full_url(), **get_params)
9198
yield request
9299

93100
return _get_url_with_session()

tools/pyosmium-get-changes

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ cookies to the server and will save received cookies to the jar file.
2727

2828
from argparse import ArgumentParser, RawDescriptionHelpFormatter, ArgumentTypeError
2929
import datetime as dt
30-
import socket
3130
from osmium.replication import server as rserv
3231
from osmium.replication import newest_change_from_file
3332
from osmium.replication.utils import get_replication_header
@@ -211,9 +210,10 @@ def main(args):
211210
or 'https://planet.osm.org/replication/minute/'
212211
logging.info("Using replication server at %s" % url)
213212

214-
socket.setdefaulttimeout(options.socket_timeout)
213+
extra_request_params = dict(stream=True,
214+
timeout=options.socket_timeout or None)
215215

216-
with rserv.ReplicationServer(url) as svr:
216+
with rserv.ReplicationServer(url, extra_request_params=extra_request_params) as svr:
217217
if options.cookie is not None:
218218
# According to the documentation, the cookie jar loads the file only if FileCookieJar.load is called.
219219
cookie_jar = cookiejarlib.MozillaCookieJar(options.cookie)

tools/pyosmium-up-to-date

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import re
3535
import sys
3636
import traceback
3737
import logging
38-
import socket
3938

4039
from argparse import ArgumentParser, RawDescriptionHelpFormatter
4140
import datetime as dt
@@ -70,7 +69,10 @@ def update_from_osm_server(ts, options):
7069

7170
def update_from_custom_server(url, seq, ts, options):
7271
"""Update from a custom URL, simply using the diff sequence as is."""
73-
with rserv.ReplicationServer(url, "osc.gz") as svr:
72+
extra_request_params = dict(stream=True,
73+
timeout=options.socket_timeout or None)
74+
75+
with rserv.ReplicationServer(url, "osc.gz", extra_request_params=extra_request_params) as svr:
7476
if options.cookie is not None:
7577
# According to the documentation, the cookie jar loads the file only if FileCookieJar.load is called.
7678
cookie_jar = cookiejarlib.MozillaCookieJar(options.cookie)
@@ -244,8 +246,6 @@ if __name__ == '__main__':
244246
options = get_arg_parser(from_main=True).parse_args()
245247
log.setLevel(max(3 - options.loglevel, 0) * 10)
246248

247-
socket.setdefaulttimeout(options.socket_timeout)
248-
249249
try:
250250
url, seq, ts = compute_start_point(options)
251251
except RuntimeError as e:

0 commit comments

Comments
 (0)