2022-06-07 12:16:23 -04:00
|
|
|
# SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2018-07-30 12:10:10 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2019-06-25 15:33:55 -04:00
|
|
|
import socket
|
2021-01-25 21:49:01 -05:00
|
|
|
|
2020-05-07 06:47:46 -04:00
|
|
|
try:
|
|
|
|
from http.client import HTTPConnection, HTTPSConnection
|
|
|
|
except ImportError:
|
|
|
|
# Python 2 fallback
|
2022-06-07 12:16:23 -04:00
|
|
|
from httplib import HTTPConnection, HTTPSConnection # type: ignore
|
2018-07-30 12:10:10 -04:00
|
|
|
|
2023-07-18 04:10:59 -04:00
|
|
|
from utils import str_to_bytes
|
|
|
|
|
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:
|
2022-06-22 05:44:19 -04:00
|
|
|
raise RuntimeError(f'Unable to resolve hostname: {hostname}')
|
2019-06-25 15:33:55 -04:00
|
|
|
|
2020-05-07 06:47:46 -04:00
|
|
|
if ssl_context is None:
|
2021-04-07 07:43:02 -04:00
|
|
|
self.conn = HTTPConnection(hostname, timeout=60)
|
2019-06-25 15:33:55 -04:00
|
|
|
else:
|
2021-04-07 07:43:02 -04:00
|
|
|
self.conn = HTTPSConnection(hostname, context=ssl_context, timeout=60)
|
2019-07-02 10:42:47 -04:00
|
|
|
try:
|
2022-06-22 05:44:19 -04:00
|
|
|
print(f'++++ Connecting to {hostname}++++')
|
2019-07-02 10:42:47 -04:00
|
|
|
self.conn.connect()
|
|
|
|
except Exception as err:
|
2021-01-25 21:49:01 -05:00
|
|
|
raise RuntimeError('Connection Failure : ' + str(err))
|
|
|
|
self.headers = {'Content-type': 'application/x-www-form-urlencoded','Accept': 'text/plain'}
|
2018-07-30 12:10:10 -04:00
|
|
|
|
|
|
|
def _send_post_request(self, path, data):
|
2023-07-18 04:10:59 -04:00
|
|
|
data = str_to_bytes(data) if isinstance(data, str) else data
|
2018-07-30 12:10:10 -04:00
|
|
|
try:
|
2023-07-18 04:10:59 -04:00
|
|
|
self.conn.request('POST', path, data, self.headers)
|
2018-07-30 12:10:10 -04:00
|
|
|
response = self.conn.getresponse()
|
2022-10-27 07:31:29 -04:00
|
|
|
# While establishing a session, the device sends the Set-Cookie header
|
|
|
|
# with value 'session=cookie_session_id' in its first response of the session to the tool.
|
|
|
|
# To maintain the same session, successive requests from the tool should include
|
|
|
|
# an additional 'Cookie' header with the above recieved value.
|
|
|
|
for hdr_key, hdr_val in response.getheaders():
|
|
|
|
if hdr_key == 'Set-Cookie':
|
|
|
|
self.headers['Cookie'] = hdr_val
|
2018-07-30 12:10:10 -04:00
|
|
|
if response.status == 200:
|
|
|
|
return response.read().decode('latin-1')
|
|
|
|
except Exception as err:
|
2021-01-25 21:49:01 -05:00
|
|
|
raise RuntimeError('Connection Failure : ' + str(err))
|
|
|
|
raise RuntimeError('Server responded with error code ' + str(response.status))
|
2018-07-30 12:10:10 -04:00
|
|
|
|
2022-06-07 12:16:23 -04:00
|
|
|
async def send_data(self, ep_name, data):
|
2018-12-04 07:46:48 -05:00
|
|
|
return self._send_post_request('/' + ep_name, data)
|