diff --git a/py-json/LANforge/LFRequest.py b/py-json/LANforge/LFRequest.py index 3cd2ed0d..cc04de55 100644 --- a/py-json/LANforge/LFRequest.py +++ b/py-json/LANforge/LFRequest.py @@ -1,6 +1,6 @@ -#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Class holds default settings for json requests - -#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - import sys if sys.version_info[0] != 3: @@ -10,27 +10,59 @@ if sys.version_info[0] != 3: import urllib.request import json + class LFRequest: Default_Base_URL = "http://localhost:8080" requested_urls = [] + post_datas = [] default_headers = { - 'Accept': 'application/json' } - + 'Accept': 'application/json'} def __init__(self, urls): self.requested_urls.append(urls) # request first url on stack + def post(self): + try: + myrequest = urllib.request.Request(url=self.requested_urls.pop(0), headers=self.default_headers) + if ((len(self.post_datas) > 0) and (self.post_datas[0] != None)): + myrequest.data = self.post_datas.pop(0) + myrequest.headers['Content-type'] = 'application/json' + + myresponse = urllib.request.urlopen(myrequest) + # print(json_response) + return myresponse + finally: + print("Error: %s" % myresponse.error_code) + def get(self): myrequest = urllib.request.Request(url=self.requested_urls.pop(0), headers=self.default_headers) - myresponse = urllib.request.urlopen(myrequest) + myresponses = [] + try: + myresponses.append(urllib.request.urlopen(myrequest)) + return myresponses[0] + + except: + print("Error: ", sys.exc_info()[0]) + + return None + - #print(json_response) - return myresponse def getAsJson(self): - response = self.get(); - json_data = json.loads(response.read()) + responses = [] + responses.append(self.get()) + if (len(responses) < 1): + return None + + if ((responses[0] == None) or (responses[0].status_code != 200)): + print("Item not found") + return None + + json_data = json.loads(responses[0].read()) return json_data + def addPostData(self, post_data): + self.post_datas.append(post_data) + # ~LFRequest diff --git a/py-json/LANforge/__pycache__/LFRequest.cpython-36.pyc b/py-json/LANforge/__pycache__/LFRequest.cpython-36.pyc deleted file mode 100644 index 5eaa976c..00000000 Binary files a/py-json/LANforge/__pycache__/LFRequest.cpython-36.pyc and /dev/null differ diff --git a/py-json/create_sta.py b/py-json/create_sta.py new file mode 100644 index 00000000..6a923a2d --- /dev/null +++ b/py-json/create_sta.py @@ -0,0 +1,46 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# example of how to create a LANforge station - +# - +# two examples, first using the URL-Encoded POST +# second using JSON POST data +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +import sys +if sys.version_info[0] != 3: + print("This script requires Python 3") + exit() + +import json +import pprint +from LANforge import LFRequest + + +def main(): + base_url = "http://localhost:8080" + shelf_id = 1 # typicaly assume Shelf 1 + resource_id = 1 # typically you're using resource 1 in stand alone realm + radio = "wiphy0" + start_id = 200 + end_id = 202 + ssid = "jedway-wpa2-x2048-4-1" + passwd = "jedway-wpa2-x2048-4-1" + j_printer = pprint.PrettyPrinter(indent=2) + + # example 1, /cli-form/rm_vlan + # for each of the station IDs, delete them if they exist + + for i in range(start_id, end_id): + url = base_url+"/port/%s/%s/%s" % (shelf_id, resource_id, i) + print("checking for station : "+url) + lf_r = LFRequest.LFRequest(url) + json_response = lf_r.getAsJson() + if (json_response != None): + print("I would delete station %s now"%i) + + print("Next we create station %s"%i) + + print("done") + + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/py-json/show_ports.py b/py-json/show_ports.py index 727432ac..b88126a2 100644 --- a/py-json/show_ports.py +++ b/py-json/show_ports.py @@ -9,6 +9,7 @@ if sys.version_info[0] != 3: exit() import json +import pprint from LANforge import LFRequest @@ -18,7 +19,10 @@ def main(): lf_r = LFRequest.LFRequest(url) json_response = lf_r.getAsJson() - print(json_response) + #print(json_response) + j_printer = pprint.PrettyPrinter(indent=2) + for record in json_response['interfaces']: + j_printer.pprint(record) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if __name__ == "__main__":