mirror of
https://github.com/Telecominfraproject/wlan-lanforge-scripts.git
synced 2025-11-02 11:48:03 +00:00
py-json: updates, tests for port existance
This commit is contained in:
@@ -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
|
||||
|
||||
Binary file not shown.
46
py-json/create_sta.py
Normal file
46
py-json/create_sta.py
Normal 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()
|
||||
@@ -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__":
|
||||
|
||||
Reference in New Issue
Block a user