| 1 |
|
| 2 |
# |
| 3 |
# v0.2 |
| 4 |
# [email protected] |
| 5 |
# |
| 6 |
|
| 7 |
import json, urllib2, ssl |
| 8 |
|
| 9 |
def axapi_auth(host, username, password): |
| 10 |
base_uri = 'https://'+host |
| 11 |
auth_payload = {"credentials": {"username": username, "password": password}} |
| 12 |
r = axapi_action(base_uri + '/axapi/v3/auth', payload=auth_payload) |
| 13 |
signature = json.loads(r)['authresponse']['signature'] |
| 14 |
return base_uri, signature |
| 15 |
|
| 16 |
|
| 17 |
def axapi_action(uri, payload='', signature='', method='POST'): |
| 18 |
# PEP476 2.7.9+ / 3.4.3+ cert check |
| 19 |
new_context = ssl._create_unverified_context() |
| 20 |
try: |
| 21 |
if method == 'POST': |
| 22 |
req = urllib2.Request(uri) |
| 23 |
req.add_header('content-type', 'application/json') |
| 24 |
if signature: |
| 25 |
req.add_header('Authorization', 'A10 {0}'.format(signature)) |
| 26 |
response = urllib2.urlopen(req, json.dumps(payload), context=new_context) |
| 27 |
elif method == 'GET': |
| 28 |
req = urllib2.Request(uri) |
| 29 |
req.add_header('content-type', 'application/json') |
| 30 |
if signature: |
| 31 |
req.add_header('Authorization', 'A10 {0}'.format(signature)) |
| 32 |
response = urllib2.urlopen(req, context=new_context) |
| 33 |
elif method == 'DELETE': |
| 34 |
req = urllib2.Request(uri) |
| 35 |
req.add_header('content-type', 'application/json') |
| 36 |
req.get_method = lambda: 'DELETE' |
| 37 |
if signature: |
| 38 |
req.add_header('Authorization', 'A10 {0}'.format(signature)) |
| 39 |
response = urllib2.urlopen(req, context=new_context) |
| 40 |
return response.read() |
| 41 |
except Exception as e: |
| 42 |
raise |
| 43 |
|
| 44 |
|
| 45 |
|