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