-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbngblaster.py
55 lines (42 loc) · 1.59 KB
/
bngblaster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
"""
Simple BNG Blaster Controller Wrapper
Author: Christian Giese
Copyright (C) 2020-2024, RtBrick, Inc.
SPDX-License-Identifier: BSD-3-Clause
"""
import requests
import json
import os
class bngblaster(object):
""" Simple BNG Blaster Controller Wrapper """
def __init__(self, host, port=8001, test_instance="test"):
self.base_url = "http://%s:%s/api/v1/instances/%s" % (host, port, test_instance)
self.headers = {"content-type": "application/json" }
self.config = {}
def status(self) -> str:
try:
return requests.get(self.base_url).json()["status"]
except:
return "error"
def create(self, config_file):
with open(config_file, "r") as f:
self.config = json.load(f)
requests.put(self.base_url, headers=self.headers, data=json.dumps(self.config))
def delete(self):
requests.delete(self.base_url)
def start(self, arguments={}):
requests.post(self.base_url+"/_start", headers=self.headers, data=json.dumps(arguments))
def stop(self):
requests.post(self.base_url+"/_stop", headers=self.headers)
def command(self, command, arguments=None):
data = {"command": command}
if isinstance(arguments, dict):
data["arguments"] = arguments
return requests.post(self.base_url+"/_command", headers=self.headers, data=json.dumps(data)).json()
def download(self, source_file):
try:
os.remove(source_file)
except:
pass
os.system("wget %s/%s" % (self.base_url, source_file))