adder.py 2.8 KB

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