adder.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function, unicode_literals
  17. import argparse
  18. import http.client
  19. from builtins import range, str
  20. from tiny_test_fw import Utility
  21. def start_session(ip, port):
  22. return http.client.HTTPConnection(ip, int(port), timeout=15)
  23. def end_session(conn):
  24. conn.close()
  25. def getreq(conn, path, verbose=False):
  26. conn.request('GET', path)
  27. resp = conn.getresponse()
  28. data = resp.read()
  29. if verbose:
  30. Utility.console_log('GET : ' + path)
  31. Utility.console_log('Status : ' + resp.status)
  32. Utility.console_log('Reason : ' + resp.reason)
  33. Utility.console_log('Data length : ' + str(len(data)))
  34. Utility.console_log('Data content : ' + data)
  35. return data
  36. def postreq(conn, path, data, verbose=False):
  37. conn.request('POST', path, data)
  38. resp = conn.getresponse()
  39. data = resp.read()
  40. if verbose:
  41. Utility.console_log('POST : ' + data)
  42. Utility.console_log('Status : ' + resp.status)
  43. Utility.console_log('Reason : ' + resp.reason)
  44. Utility.console_log('Data length : ' + str(len(data)))
  45. Utility.console_log('Data content : ' + data)
  46. return data
  47. def putreq(conn, path, body, verbose=False):
  48. conn.request('PUT', path, body)
  49. resp = conn.getresponse()
  50. data = resp.read()
  51. if verbose:
  52. Utility.console_log('PUT : ' + path, body)
  53. Utility.console_log('Status : ' + resp.status)
  54. Utility.console_log('Reason : ' + resp.reason)
  55. Utility.console_log('Data length : ' + str(len(data)))
  56. Utility.console_log('Data content : ' + data)
  57. return data
  58. if __name__ == '__main__':
  59. # Configure argument parser
  60. parser = argparse.ArgumentParser(description='Run HTTPd Test')
  61. parser.add_argument('IP', metavar='IP', type=str, help='Server IP')
  62. parser.add_argument('port', metavar='port', type=str, help='Server port')
  63. parser.add_argument('N', metavar='integer', type=int, help='Integer to sum upto')
  64. args = vars(parser.parse_args())
  65. # Get arguments
  66. ip = args['IP']
  67. port = args['port']
  68. N = args['N']
  69. # Establish HTTP connection
  70. Utility.console_log('Connecting to => ' + ip + ':' + port)
  71. conn = start_session(ip, port)
  72. # Reset adder context to specified value(0)
  73. # -- Not needed as new connection will always
  74. # -- have zero value of the accumulator
  75. Utility.console_log('Reset the accumulator to 0')
  76. putreq(conn, '/adder', str(0))
  77. # Sum numbers from 1 to specified value(N)
  78. Utility.console_log('Summing numbers from 1 to ' + str(N))
  79. for i in range(1, N + 1):
  80. postreq(conn, '/adder', str(i))
  81. # Fetch the result
  82. Utility.console_log('Result :' + getreq(conn, '/adder'))
  83. # Close HTTP connection
  84. end_session(conn)