adder.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. #
  3. # SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  4. # SPDX-License-Identifier: Apache-2.0
  5. import argparse
  6. import http.client
  7. import logging
  8. def start_session(ip, port):
  9. return http.client.HTTPConnection(ip, int(port), timeout=15)
  10. def end_session(conn):
  11. conn.close()
  12. def getreq(conn, path, verbose=False):
  13. conn.request('GET', path)
  14. resp = conn.getresponse()
  15. data = resp.read()
  16. if verbose:
  17. logging.info('GET : {}'.format(path))
  18. logging.info('Status : {}'.format(resp.status))
  19. logging.info('Reason : {}'.format(resp.reason))
  20. logging.info('Data length : {}'.format(len(data)))
  21. logging.info('Data content : {}'.format(data))
  22. return data
  23. def postreq(conn, path, data, verbose=False):
  24. conn.request('POST', path, data)
  25. resp = conn.getresponse()
  26. data = resp.read()
  27. if verbose:
  28. logging.info('POST : {}'.format(data))
  29. logging.info('Status : {}'.format(resp.status))
  30. logging.info('Reason : {}'.format(resp.reason))
  31. logging.info('Data length : {}'.format(len(data)))
  32. logging.info('Data content : {}'.format(data))
  33. return data
  34. def putreq(conn, path, body, verbose=False):
  35. conn.request('PUT', path, body)
  36. resp = conn.getresponse()
  37. data = resp.read()
  38. if verbose:
  39. logging.info('PUT : {} {}'.format(path, body))
  40. logging.info('Status : {}'.format(resp.status))
  41. logging.info('Reason : {}'.format(resp.reason))
  42. logging.info('Data length : {}'.format(len(data)))
  43. logging.info('Data content : {}'.format(data))
  44. return data
  45. if __name__ == '__main__':
  46. # Configure argument parser
  47. parser = argparse.ArgumentParser(description='Run HTTPd Test')
  48. parser.add_argument('IP', metavar='IP', type=str, help='Server IP')
  49. parser.add_argument('port', metavar='port', type=str, help='Server port')
  50. parser.add_argument('N', metavar='integer', type=int, help='Integer to sum upto')
  51. args = vars(parser.parse_args())
  52. # Get arguments
  53. ip = args['IP']
  54. port = args['port']
  55. N = args['N']
  56. # Establish HTTP connection
  57. logging.info('Connecting to => ' + ip + ':' + port)
  58. conn = start_session(ip, port)
  59. # Reset adder context to specified value(0)
  60. # -- Not needed as new connection will always
  61. # -- have zero value of the accumulator
  62. logging.info('Reset the accumulator to 0')
  63. putreq(conn, '/adder', str(0))
  64. # Sum numbers from 1 to specified value(N)
  65. logging.info('Summing numbers from 1 to {}'.format(N))
  66. for i in range(1, N + 1):
  67. postreq(conn, '/adder', str(i))
  68. # Fetch the result
  69. logging.info('Result :{}'.format(getreq(conn, '/adder')))
  70. # Close HTTP connection
  71. end_session(conn)