adder.py 2.9 KB

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