-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevClient.py
More file actions
91 lines (71 loc) · 2.85 KB
/
devClient.py
File metadata and controls
91 lines (71 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import http
import logging
import socket
import warnings
import requests
# from requests.packages.urllib3.exceptions import ConnectionError
# from requests.packages.urllib3.exceptions import RetryError
# from requests.packages.urllib3.exceptions import TimeoutError
# from requests.packages.urllib3.exceptions import SSLError as _SSLError
# from requests.packages.urllib3.exceptions import HTTPError as _HTTPError
from requests.exceptions import ConnectionError, RetryError, Timeout, SSLError, HTTPError
# This is well explained by Ben Hoey at:
# https://bhoey.com/blog/better-debug-logging-for-the-python-requests-library/
# This is his excellent approach to requests debug logging. Thank you Ben.
# It is especially valuable when attempting to deal with a
# poorly documented or otherwise opaque API.
# I have it here just as a reminder to me...
# Set up logging to a file
logging.basicConfig(filename="devClient.log", level=logging.DEBUG)
# Using __name__ as the name of the logger makes sure that the
# logger hierarchy reflects the python module hierarchy
logger = logging.getLogger(__name__)
# Turn on global debugging for the HTTPConnection class
http.client.HTTPConnection.debuglevel = 1
# Redirect print to the debug logger
def debugLog(*args):
logger.debug(" ".join(args))
http.client.print = debugLog
# Assumes you have something listening for tests
url = "http://localhost:8080/test"
logger.info("START HTTP GET Test")
try:
# Test HTTP GET
resp = requests.get(url, timeout=5)
if resp.status_code == 408:
logger.exception(f"Request Timeout: {str(resp.status_code)}")
# Note: Removed the two exceptions below because they were too broad for my use cases.
# except OSError as msg:
# logger.exception(f"OSError: Something bad happened. Hostname correct? Network OK? {msg}")
# except socket.error as sockerr:
# logger.exception(f"Something bad happened. Hostname correct? Network OK? {sockerr}")
except ConnectionError as e:
logger.exception(f"{e}")
except Timeout as e:
'''Catching this error will catch both
:exc:`~requests.exceptions.ConnectTimeout` and
:exc:`~requests.exceptions.ReadTimeout` errors.
'''
logger.exception(f"{e}")
except RetryError as e:
logger.exception(f"{e}")
except (SSLError, HTTPError) as e:
if isinstance(e, SSLError):
logger.exception(f"{e}")
elif isinstance(e, TimeoutError):
logger.exception(f"{e}")
else:
logger.exception(f"Request timed out. Not SSLError or TimeoutError: {e}")
except Exception as e:
logger.exception(f"The request failed - why?: {e}")
logger.info("START HTTP POST Test")
# Test HTTP POST
# ToDo: Wrap the usage below in a try/except
"""
# something like this:
try:
request_code()
except Exception:
logger.exception("The request failed.")
"""
resp = requests.post(url, data='POST Testing data', timeout=5)