adder.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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
  17. from __future__ import unicode_literals
  18. from future import standard_library
  19. standard_library.install_aliases()
  20. from builtins import str
  21. from builtins import range
  22. import http.client
  23. import argparse
  24. def start_session (ip, port):
  25. return http.client.HTTPConnection(ip, int(port), timeout=15)
  26. def end_session (conn):
  27. conn.close()
  28. def getreq (conn, path, verbose = False):
  29. conn.request("GET", path)
  30. resp = conn.getresponse()
  31. data = resp.read()
  32. if verbose:
  33. Utility.console_log("GET : " + path)
  34. Utility.console_log("Status : " + resp.status)
  35. Utility.console_log("Reason : " + resp.reason)
  36. Utility.console_log("Data length : " + str(len(data)))
  37. Utility.console_log("Data content : " + data)
  38. return data
  39. def postreq (conn, path, data, verbose = False):
  40. conn.request("POST", path, data)
  41. resp = conn.getresponse()
  42. data = resp.read()
  43. if verbose:
  44. Utility.console_log("POST : " + data)
  45. Utility.console_log("Status : " + resp.status)
  46. Utility.console_log("Reason : " + resp.reason)
  47. Utility.console_log("Data length : " + str(len(data)))
  48. Utility.console_log("Data content : " + data)
  49. return data
  50. def putreq (conn, path, body, verbose = False):
  51. conn.request("PUT", path, body)
  52. resp = conn.getresponse()
  53. data = resp.read()
  54. if verbose:
  55. Utility.console_log("PUT : " + path, body)
  56. Utility.console_log("Status : " + resp.status)
  57. Utility.console_log("Reason : " + resp.reason)
  58. Utility.console_log("Data length : " + str(len(data)))
  59. Utility.console_log("Data content : " + data)
  60. return data
  61. if __name__ == '__main__':
  62. # Configure argument parser
  63. parser = argparse.ArgumentParser(description='Run HTTPd Test')
  64. parser.add_argument('IP' , metavar='IP' , type=str, help='Server IP')
  65. parser.add_argument('port', metavar='port', type=str, help='Server port')
  66. parser.add_argument('N' , metavar='integer', type=int, help='Integer to sum upto')
  67. args = vars(parser.parse_args())
  68. # Get arguments
  69. ip = args['IP']
  70. port = args['port']
  71. N = args['N']
  72. # Establish HTTP connection
  73. Utility.console_log("Connecting to => " + ip + ":" + port)
  74. conn = start_session (ip, port)
  75. # Reset adder context to specified value(0)
  76. # -- Not needed as new connection will always
  77. # -- have zero value of the accumulator
  78. Utility.console_log("Reset the accumulator to 0")
  79. putreq (conn, "/adder", str(0))
  80. # Sum numbers from 1 to specified value(N)
  81. Utility.console_log("Summing numbers from 1 to " + str(N))
  82. for i in range(1, N+1):
  83. postreq (conn, "/adder", str(i))
  84. # Fetch the result
  85. Utility.console_log("Result :" + getreq (conn, "/adder"))
  86. # Close HTTP connection
  87. end_session (conn)