This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
_transport.py
107 lines (79 loc) · 3.18 KB
/
_transport.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Methods to interact with higher level network protocols.
Transport will provide methods to send and receive data using higher level
protocols like HTTP, mDNS, etc.
This module is dependent on modules from the LogoCert package.
"""
import socket # In order to set a default timeout.
import requests
from _config import Constants
class Transport(object):
"""Send and receive network messages and communication."""
def __init__(self, logger):
"""Get a reference to a logger object and JsonParser.
Args:
logger: initialized logger object.
"""
self.logger = logger
socket.setdefaulttimeout(Constants.URL['TIMEOUT'])
def HTTPPost(self, url, headers=None, params=None, data=None, files=None):
"""Send a HTTP Post Request to a remote server.
Args:
url: string, url to send request to.
headers: dict, key/value pairs of HTTP header.
params: key/value pairs of parameters.
data: data to post
files: file to post
Returns:
dict: Response object, with keys code, headers, and data.
"""
response = requests.post(url, headers=headers, params=params, data=data,
files=files)
if response is None:
self.logger.info('HTTP(S) POST to %s failed', url)
return None
self.LogResponseData(response)
return response
def HTTPGet(self, url, headers=None, params=None):
"""Send a HTTP Get Request to a remote server.
Args:
url: string, url to send request to.
headers: dict, key/value pairs of HTTP header.
params: key/value pairs of parameters.
Returns:
dict: Response object, with keys code, headers, and data.
"""
response = requests.get(url, headers=headers, params=params)
if response is None:
self.logger.info('HTTP(S) GET for %s failed', url)
return None
self.LogResponseData(response)
return response
def LogResponseData(self, response):
"""Log all response headers and data.
If this function is called and the log level is not debug, this method will
only log the return code.
Args:
response: Response object from the requests module.
"""
self.logger.debug('HTTP device return code: %s', response.status_code)
self.logger.debug('Headers returned from query:')
for k,v in response.headers.iteritems():
self.logger.debug('Header Key: %s\nHeader Value: %s', k, v)
self.logger.debug('Response Data:')
try:
info = response.json()
except ValueError:
self.logger.info('No JSON object in response')
else:
for k in info:
self.logger.debug('Data item: %s\nData Value: %s', k, info[k])