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