py-json: updates, tests for port existance

This commit is contained in:
Jed Reynolds
2019-10-28 11:26:33 -07:00
parent af606e9bb8
commit a252b641a3
4 changed files with 92 additions and 10 deletions

View File

@@ -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

46
py-json/create_sta.py Normal file
View File

@@ -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()

View File

@@ -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__":