Coverage for pyguymer3/download.py: 7%

14 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-08 18:47 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def download( 

5 sess, 

6 method, 

7 url, 

8 /, 

9 *, 

10 cookies = None, 

11 headers = None, 

12 timeout = 10.0, 

13 verify = True, 

14): 

15 """Call a URL and return the response 

16 

17 This function performs a HTTP call, with a GET/POST/PUT/HEAD/OPTIONS method, 

18 on a URL and returns the response. 

19 

20 Parameters 

21 ---------- 

22 sess : requests.sessions.Session 

23 the :mod:`requests` session to use 

24 meth : str 

25 the method 

26 url : str 

27 the URL 

28 cookies : dict, optional 

29 the cookie jar 

30 headers : dict, optional 

31 extra headers to send 

32 timeout : float, optional 

33 the timeout of the GET/POST/PUT/HEAD/OPTIONS request 

34 verify : bool, optional 

35 verify the server's certificates 

36 

37 Returns 

38 ------- 

39 resp : bool, requests.models.Response 

40 `False` if unsuccessful or a `requests.models.Response` if successful 

41 

42 Notes 

43 ----- 

44 Copyright 2017 Thomas Guymer [1]_ 

45 

46 References 

47 ---------- 

48 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

49 """ 

50 

51 # Populate default values ... 

52 if cookies is None: 

53 cookies = {} 

54 if headers is None: 

55 headers = {} 

56 

57 # ************************************************************************** 

58 

59 # Catch common errors ... 

60 if "&" in url: 

61 raise Exception("the URL contains \"&\"") from None 

62 

63 # Try to download the URL and catch common errors ... 

64 try: 

65 resp = sess.request( 

66 method, 

67 url, 

68 cookies = cookies, 

69 headers = headers, 

70 timeout = timeout, 

71 verify = verify, 

72 ) 

73 except: 

74 return False 

75 

76 # Exit if the response was bad ... 

77 if resp.status_code != 200: 

78 return False 

79 

80 # Return answer ... 

81 return resp