2018-07-03 06:57:41 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2022-05-10 06:46:58 -04:00
|
|
|
# SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2018-07-03 06:57:41 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
from __future__ import print_function, unicode_literals
|
|
|
|
|
2018-07-03 06:57:41 -04:00
|
|
|
import argparse
|
2021-01-25 21:49:01 -05:00
|
|
|
import http.client
|
2022-05-10 06:46:58 -04:00
|
|
|
import logging
|
2021-01-25 21:49:01 -05:00
|
|
|
from builtins import range, str
|
2019-11-26 22:58:07 -05:00
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
|
|
|
|
def start_session(ip, port):
|
2018-09-10 09:13:47 -04:00
|
|
|
return http.client.HTTPConnection(ip, int(port), timeout=15)
|
2018-07-03 06:57:41 -04:00
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
|
|
|
|
def end_session(conn):
|
2018-07-03 06:57:41 -04:00
|
|
|
conn.close()
|
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
|
|
|
|
def getreq(conn, path, verbose=False):
|
2021-01-25 21:49:01 -05:00
|
|
|
conn.request('GET', path)
|
2018-07-03 06:57:41 -04:00
|
|
|
resp = conn.getresponse()
|
|
|
|
data = resp.read()
|
|
|
|
if verbose:
|
2022-05-10 06:46:58 -04:00
|
|
|
logging.info('GET : {}'.format(path))
|
|
|
|
logging.info('Status : {}'.format(resp.status))
|
|
|
|
logging.info('Reason : {}'.format(resp.reason))
|
|
|
|
logging.info('Data length : {}'.format(len(data)))
|
|
|
|
logging.info('Data content : {}'.format(data))
|
2018-07-03 06:57:41 -04:00
|
|
|
return data
|
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
|
|
|
|
def postreq(conn, path, data, verbose=False):
|
2021-01-25 21:49:01 -05:00
|
|
|
conn.request('POST', path, data)
|
2018-07-03 06:57:41 -04:00
|
|
|
resp = conn.getresponse()
|
|
|
|
data = resp.read()
|
|
|
|
if verbose:
|
2022-05-10 06:46:58 -04:00
|
|
|
logging.info('POST : {}'.format(data))
|
|
|
|
logging.info('Status : {}'.format(resp.status))
|
|
|
|
logging.info('Reason : {}'.format(resp.reason))
|
|
|
|
logging.info('Data length : {}'.format(len(data)))
|
|
|
|
logging.info('Data content : {}'.format(data))
|
2018-07-03 06:57:41 -04:00
|
|
|
return data
|
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
|
|
|
|
def putreq(conn, path, body, verbose=False):
|
2021-01-25 21:49:01 -05:00
|
|
|
conn.request('PUT', path, body)
|
2018-07-03 06:57:41 -04:00
|
|
|
resp = conn.getresponse()
|
|
|
|
data = resp.read()
|
|
|
|
if verbose:
|
2022-05-10 06:46:58 -04:00
|
|
|
logging.info('PUT : {} {}'.format(path, body))
|
|
|
|
logging.info('Status : {}'.format(resp.status))
|
|
|
|
logging.info('Reason : {}'.format(resp.reason))
|
|
|
|
logging.info('Data length : {}'.format(len(data)))
|
|
|
|
logging.info('Data content : {}'.format(data))
|
2018-07-03 06:57:41 -04:00
|
|
|
return data
|
|
|
|
|
2018-12-04 02:32:48 -05:00
|
|
|
|
2018-07-03 06:57:41 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
# Configure argument parser
|
|
|
|
parser = argparse.ArgumentParser(description='Run HTTPd Test')
|
2018-12-04 02:32:48 -05:00
|
|
|
parser.add_argument('IP', metavar='IP', type=str, help='Server IP')
|
2018-07-03 06:57:41 -04:00
|
|
|
parser.add_argument('port', metavar='port', type=str, help='Server port')
|
2018-12-04 02:32:48 -05:00
|
|
|
parser.add_argument('N', metavar='integer', type=int, help='Integer to sum upto')
|
2018-07-03 06:57:41 -04:00
|
|
|
args = vars(parser.parse_args())
|
|
|
|
|
|
|
|
# Get arguments
|
|
|
|
ip = args['IP']
|
|
|
|
port = args['port']
|
|
|
|
N = args['N']
|
|
|
|
|
|
|
|
# Establish HTTP connection
|
2022-05-10 06:46:58 -04:00
|
|
|
logging.info('Connecting to => ' + ip + ':' + port)
|
2018-12-04 02:32:48 -05:00
|
|
|
conn = start_session(ip, port)
|
2018-07-03 06:57:41 -04:00
|
|
|
|
|
|
|
# Reset adder context to specified value(0)
|
|
|
|
# -- Not needed as new connection will always
|
|
|
|
# -- have zero value of the accumulator
|
2022-05-10 06:46:58 -04:00
|
|
|
logging.info('Reset the accumulator to 0')
|
2021-01-25 21:49:01 -05:00
|
|
|
putreq(conn, '/adder', str(0))
|
2018-07-03 06:57:41 -04:00
|
|
|
|
|
|
|
# Sum numbers from 1 to specified value(N)
|
2022-05-10 06:46:58 -04:00
|
|
|
logging.info('Summing numbers from 1 to {}'.format(N))
|
2018-12-04 02:32:48 -05:00
|
|
|
for i in range(1, N + 1):
|
2021-01-25 21:49:01 -05:00
|
|
|
postreq(conn, '/adder', str(i))
|
2018-07-03 06:57:41 -04:00
|
|
|
|
|
|
|
# Fetch the result
|
2022-05-10 06:46:58 -04:00
|
|
|
logging.info('Result :{}'.format(getreq(conn, '/adder')))
|
2018-07-03 06:57:41 -04:00
|
|
|
|
|
|
|
# Close HTTP connection
|
2018-12-04 02:32:48 -05:00
|
|
|
end_session(conn)
|