2018-07-30 12:10:10 -04:00
|
|
|
# Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
from future.utils import tobytes
|
|
|
|
|
2019-06-25 15:33:55 -04:00
|
|
|
import socket
|
2020-05-07 06:47:46 -04:00
|
|
|
try:
|
|
|
|
from http.client import HTTPConnection, HTTPSConnection
|
|
|
|
except ImportError:
|
|
|
|
# Python 2 fallback
|
|
|
|
from httplib import HTTPConnection, HTTPSConnection
|
2018-07-30 12:10:10 -04:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
from .transport import Transport
|
|
|
|
|
2018-07-30 12:10:10 -04:00
|
|
|
|
2019-06-25 15:33:55 -04:00
|
|
|
class Transport_HTTP(Transport):
|
2020-05-07 06:47:46 -04:00
|
|
|
def __init__(self, hostname, ssl_context=None):
|
2019-06-25 15:33:55 -04:00
|
|
|
try:
|
|
|
|
socket.gethostbyname(hostname.split(':')[0])
|
|
|
|
except socket.gaierror:
|
|
|
|
raise RuntimeError("Unable to resolve hostname :" + hostname)
|
|
|
|
|
2020-05-07 06:47:46 -04:00
|
|
|
if ssl_context is None:
|
2020-06-26 00:21:11 -04:00
|
|
|
self.conn = HTTPConnection(hostname, timeout=45)
|
2019-06-25 15:33:55 -04:00
|
|
|
else:
|
2020-06-26 00:21:11 -04:00
|
|
|
self.conn = HTTPSConnection(hostname, context=ssl_context, timeout=45)
|
2019-07-02 10:42:47 -04:00
|
|
|
try:
|
|
|
|
print("Connecting to " + hostname)
|
|
|
|
self.conn.connect()
|
|
|
|
except Exception as err:
|
|
|
|
raise RuntimeError("Connection Failure : " + str(err))
|
2018-07-30 12:10:10 -04:00
|
|
|
self.headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
|
|
|
|
|
|
|
|
def _send_post_request(self, path, data):
|
|
|
|
try:
|
|
|
|
self.conn.request("POST", path, tobytes(data), self.headers)
|
|
|
|
response = self.conn.getresponse()
|
|
|
|
if response.status == 200:
|
|
|
|
return response.read().decode('latin-1')
|
|
|
|
except Exception as err:
|
|
|
|
raise RuntimeError("Connection Failure : " + str(err))
|
|
|
|
raise RuntimeError("Server responded with error code " + str(response.status))
|
|
|
|
|
|
|
|
def send_data(self, ep_name, data):
|
2018-12-04 07:46:48 -05:00
|
|
|
return self._send_post_request('/' + ep_name, data)
|