From cb8353336e58658276a6ccffdc3c39d7716cea13 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 6 Jul 2019 02:36:44 -0500 Subject: [PATCH] refactored timeouts, added http server for testing --- streamedrequests/get.py | 10 ++++------ streamedrequests/post.py | 5 +++-- streamedrequests/setuptimeout.py | 23 +++++++++++++++++++++++ tests/test_basic.py | 25 +++++++++++++++++++------ 4 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 streamedrequests/setuptimeout.py diff --git a/streamedrequests/get.py b/streamedrequests/get.py index b3f1be6..cd7440d 100644 --- a/streamedrequests/get.py +++ b/streamedrequests/get.py @@ -16,19 +16,17 @@ along with this program. If not, see . ''' import requests -from . import exceptions, responsesize, dodownload +from . import exceptions, responsesize, dodownload, setuptimeout def get(url, query_parameters=None, request_headers=None, sync=True, max_size=0, chunk_size=1000, connect_timeout=60, stream_timeout=0, - proxy=None, callback=None, allow_redirects=True): + proxy={}, callback=None, allow_redirects=True): chunk_count = responsesize.SizeValidator(max_size) # Class to verify if the stream is staying within the max_size - timeouts = (connect_timeout, stream_timeout) - if stream_timeout == 0: # If timeout for stream is default, use connect timeout for both - timeouts = connect_timeout + timeouts = setuptimeout.__setup_timeout(connect_timeout, stream_timeout) req = requests.get(url, params=query_parameters, headers=request_headers, - timeout=timeouts, stream=True, allow_redirects=allow_redirects) + timeout=timeouts, stream=True, allow_redirects=allow_redirects, proxies=proxy) return dodownload.__do_download(req, max_size, chunk_size, callback, sync) \ No newline at end of file diff --git a/streamedrequests/post.py b/streamedrequests/post.py index 163299c..3ccad81 100644 --- a/streamedrequests/post.py +++ b/streamedrequests/post.py @@ -17,5 +17,6 @@ ''' import requests def post(url, post_data=None, request_headers=None, sync=True, - max_size=0, connect_timeout=0, stream_timeout=0, proxy=None): - return \ No newline at end of file + max_size=0, chunk_size=1000, connect_timeout=60, stream_timeout=0, + proxy={}, callback=None, allow_redirects=True): + return \ No newline at end of file diff --git a/streamedrequests/setuptimeout.py b/streamedrequests/setuptimeout.py new file mode 100644 index 0000000..0d1b527 --- /dev/null +++ b/streamedrequests/setuptimeout.py @@ -0,0 +1,23 @@ +''' + StreamedRequests. A simple library for streaming HTTP requests + Copyright (C) 2019 Kevin Froman https://chaoswebs.net/ + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +''' +def __setup_timeout(connect_timeout, stream_timeout): + timeouts = (connect_timeout, stream_timeout) + + if stream_timeout == 0: # If timeout for stream is default, use connect timeout for both + timeouts = connect_timeout + return timeouts \ No newline at end of file diff --git a/tests/test_basic.py b/tests/test_basic.py index e8bb4b1..2163417 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -15,34 +15,47 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import sys, os, unittest +import sys, os, unittest, threading +from http.server import HTTPServer, SimpleHTTPRequestHandler +import requests sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../") import streamedrequests def get_test_id(): return str(uuid.uuid4()) + '.dat' +def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler): + server_address = ('127.0.0.1', 8000) + httpd = server_class(server_address, handler_class) + httpd.serve_forever() + def _test_callback(text): - print('got', text) + return + #print('got', text) class TestInit(unittest.TestCase): + def test_requests(self): + requests.get('http://127.0.0.1:8000/') + def test_basic(self): - streamedrequests.get('https://example.com/') + streamedrequests.get('http://127.0.0.1:8000/') def test_callback(self): pass - streamedrequests.get('https://example.com/', chunk_size=1, callback=_test_callback) + streamedrequests.get('http://127.0.0.1:8000/', chunk_size=1, callback=_test_callback) def test_async(self): - streamedrequests.get('https://example.com/', chunk_size=1, callback=_test_callback, sync=False) + streamedrequests.get('http://127.0.0.1:8000/', chunk_size=1, callback=_test_callback, sync=False) def test_zero_chunk_size(self): try: - streamedrequests.get('https://example.com/', chunk_size=0) + streamedrequests.get('http://127.0.0.1:8000/', chunk_size=0) except ValueError: pass else: self.assertTrue(failUnless) +threading.Thread(target=run, daemon=True).start() + unittest.main() \ No newline at end of file