Created
May 19, 2021 10:00
-
-
Save Robokishan/b8ef572b0ff3e539e7eb93031c7fe43d to your computer and use it in GitHub Desktop.
python example for sending bulk request to server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import requests | |
import threading | |
import time | |
class Manager: | |
server = os.getenv('GATEWAY_URL', "http://p1007.local:8080") | |
config_api = os.getenv('GATEWAY_CONFIG_API', "/query/config") | |
data_api = os.getenv('GATEWAY_DATA_API', "/query/data") | |
init_api = os.getenv('GATEWAY_INIT_API', "/config/init") | |
def __init__(self): | |
print ("server",self.server) | |
print ("config_api",self.config_api) | |
print ("data_api",self.data_api) | |
print ("access_token",self.access_token) | |
print ("config_headers",self.config_headers) | |
def getConfig(self) -> json: | |
try: | |
r = requests.get(url=(self.server+self.config_api)) | |
data = r.json() | |
print("[MANAGER] Config : {}".format(json.dumps(data))) | |
return (data,r.status_code) | |
except Exception as e: | |
print(e) | |
return ({},404) | |
def sendData(self,payload: json): | |
try: | |
cloud_payload = {} | |
cloud_payload['d'] = payload | |
print("[MANAGER] Sending Data", cloud_payload) | |
r = requests.post(self.server+self.data_api, headers=self.config_headers, data=json.dumps(cloud_payload)) | |
print(r.status_code) | |
return r.status_code | |
except Exception as e: | |
print(e) | |
return ({},404) | |
def sendInit(self, payload: json): | |
try: | |
print("[MANAGER] Sending Init", payload) | |
r = requests.put(self.server+self.init_api, headers=self.config_headers, data=json.dumps(payload)) | |
print(f"[INIT]: {r.json()}") | |
return r.status_code | |
except Exception as e: | |
print(e) | |
return ({},404) | |
def main(): | |
manager = Manager() | |
counter = 1000 | |
lastPublishMillis = int(time.time()*1000) | |
prev_publishMillis = 0 | |
payload = { | |
"temp": 0, | |
"hum": 0, | |
"volt": 0, | |
"current": 0, | |
"bs": 0, | |
"s1": 0, | |
"s2": 0, | |
"s3": 0, | |
"sg": 0 | |
} | |
thread_list = [] | |
for count in range(0,counter): | |
if prev_publishMillis == lastPublishMillis: | |
print(f"Equals found {lastPublishMillis} {prev_publishMillis}") | |
lastPublishMillis+10 | |
exit(2) | |
payload.update({"t":lastPublishMillis}) | |
thread = threading.Thread(target=manager.sendData, args=(payload,),daemon=True) | |
thread.start() | |
thread_list.append(thread) | |
prev_publishMillis = lastPublishMillis | |
lastPublishMillis=lastPublishMillis+2 | |
while 1: | |
break_main_loop = True | |
for thread in thread_list: | |
if thread.is_alive() == True: | |
break_main_loop = False | |
if break_main_loop == True: | |
print(f"Done {counter}") | |
exit() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment