progress on LFRequest examples, distinguishing between cli-form and cli-json submission

This commit is contained in:
Jed Reynolds
2019-10-28 17:40:33 -07:00
parent 53c3b9f595
commit 18351b93fe
2 changed files with 130 additions and 22 deletions

View File

@@ -8,6 +8,7 @@ if sys.version_info[0] != 3:
exit()
import urllib.request
import urllib.parse
import json
@@ -22,22 +23,41 @@ class LFRequest:
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'
def formPost(self):
responses = []
urlenc_data = ""
if ((len(self.post_datas) > 0) and (self.post_datas[0] != None)):
urlenc_data = urllib.parse.urlencode(self.post_datas.pop(0)).encode("utf-8")
print("data looks like:"+str(urlenc_data))
request = urllib.request.Request(url=self.requested_urls.pop(0),
data=urlenc_data,
headers=self.default_headers)
else:
request = urllib.request.Request(url=self.requested_urls.pop(0), headers=self.default_headers)
print("No data for this jsonPost?")
myresponse = urllib.request.urlopen(myrequest)
# print(json_response)
return myresponse
finally:
print("Error: %s" % myresponse.error_code)
request.headers['Content-type'] = 'application/x-www-form-urlencoded'
responses.append(urllib.request.urlopen(request))
return responses[0]
def jsonPost(self):
responses = []
if ((len(self.post_datas) > 0) and (self.post_datas[0] != None)):
request = urllib.request.Request(url=self.requested_urls.pop(0),
data=self.post_datas.pop(0),
headers=self.default_headers)
else:
request = urllib.request.Request(url=self.requested_urls.pop(0), headers=self.default_headers)
print("No data for this jsonPost?")
request.headers['Content-type'] = 'application/json'
responses.append(urllib.request.urlopen(request))
return responses[0]
def get(self):
myrequest = urllib.request.Request(url=self.requested_urls.pop(0), headers=self.default_headers)
myresponses = []
#print(responses[0].__dict__.keys()) is how you would see parts of response
try:
myresponses.append(urllib.request.urlopen(myrequest))
return myresponses[0]
@@ -48,14 +68,13 @@ class LFRequest:
return None
def getAsJson(self):
responses = []
responses.append(self.get())
if (len(responses) < 1):
return None
if ((responses[0] == None) or (responses[0].status_code != 200)):
if ((responses[0] == None) or (responses[0].status != 200)):
print("Item not found")
return None

View File

@@ -13,7 +13,6 @@ import json
import pprint
from LANforge import LFRequest
def main():
base_url = "http://localhost:8080"
shelf_id = 1 # typicaly assume Shelf 1
@@ -21,25 +20,115 @@ def main():
radio = "wiphy0"
start_id = 200
end_id = 202
padding_number = 10000 # the first digit of this will be deleted
ssid = "jedway-wpa2-x2048-4-1"
passwd = "jedway-wpa2-x2048-4-1"
passphrase = "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
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# example 2 -
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# uses URL /cli-form/rm_vlan
# for each of the station names, delete them if they exist
# NOTE: the ID field of the EID is ephemeral, so best stick to
# requesting the station name. The station name can be formatted
# with leading zeros, sta00001 is legal and != {sta0001, sta001, sta01, or sta1}
for i in range(start_id, end_id):
url = base_url+"/port/%s/%s/%s" % (shelf_id, resource_id, i)
for i in range((padding_number+start_id), (padding_number+end_id)):
sta_name = "sta"+str(i)[1:]
url = base_url+"/port/%s/%s/%s" % (shelf_id, resource_id, sta_name)
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("I would delete station %s now"%sta_name)
lf_r = LFRequest.LFRequest(base_url+"/cli-form/rm_vlan")
lf_r.addPostData( {
"shelf":1,
"resource": resource_id,
"port": "sta%s"%i
})
json_response = lf_r.formPost()
print(json_response)
print("Next we create station %s"%sta_name)
lf_r = LFRequest.LFRequest(base_url+"/cli-form/add_sta")
# flags are a decimal equivalent of a hexadecimal bitfield
# you can submit as either 0x(hex) or (dec)
# a helper page is available at http://localhost:8080/help/add_sta
#
# You can watch console output of the LANforge GUI client when you
# get errors to this command, and you can also watch the websocket
# output for a response to this command at ws://localhost:8081
# Use wsdump ws://localhost:8081/
#
# modes are listed at http://<YOUR_LANFORGE>/LANforgeDocs-5.4.1/lfcli_ug.html
# or at https://www.candelatech.com/lfcli_ug.html
#
# mac address field is a pattern for creation: entirely random mac addresses
# do not take advantage of address mask matchin in Ath10k hardware, so we developed
# this pattern to randomize a section of octets. XX: keep parent, *: randomize, and
# chars [0-9a-f]: use this digit
lf_r.addPostData( {
"shelf":1,
"resource": resource_id,
"radio": radio,
"sta_name": "sta%s"%i,
"flags":68727874560,
"ssid": ssid,
"key": passphrase,
"mac": "xx:xx:xx:*:xx",
"mode": 0,
"rate": "DEFAULT"
})
json_response = lf_r.formPost()
print(json_response)
print("done")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# example 2 -
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# uses URL /cli-jsoin/rm_vlan
# and that accepts json formatted POSTS
for i in range((padding_number+start_id), (padding_number+end_id)):
sta_name = "sta"+str(i)[1:]
url = base_url+"/port/%s/%s/%s" % (shelf_id, resource_id, sta_name)
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"%sta_name)
lf_r = LFRequest.LFRequest(base_url+"/cli-json/rm_vlan")
lf_r.addPostData( {
"shelf":1,
"resource": resource_id,
"port": "sta%s"%i
})
json_response = lf_r.jsonPost()
print(json_response)
print("Next we create station %s"%sta_name)
lf_r = LFRequest.LFRequest(base_url+"/cli-form/add_sta")
# see notes from example 1 on flags and mac address patterns
lf_r.addPostData( {
"shelf":1,
"resource": resource_id,
"radio": radio,
"sta_name": "sta%s"%i,
"flags":68727874560,
"ssid": ssid,
"key": passphrase,
"mac": "xx:xx:xx:*:xx",
"mode": 0,
"rate": "DEFAULT"
})
json_response = lf_r.jsonPost()
print(json_response)
print("done")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if __name__ == "__main__":