Codebeispiele für Python-basierte Request-Anwendungen

Die folgenden Beispiele haben keinen Anspruch auf Korrektheit bzw. Lauffähigkeit und dienen nur der Veranschaulichung.

DNS: RECORD

POST-Request

  • mehrere adreßbasierte DNS-Resource-Records eintragen
  • Erkennung von IPv4- und IPv6-Adressen
  • Nur als ‘A/AAAA-RR für Hostnamen’ eintragen (symmetrische PTR-RR’s werden hierbei automatisch angelegt)
  • Python-Codevorlage:
#!/usr/bin/python

import requests
import json
import ipaddress

certfilename = '/home/username/.ssl/mycert.pem'
root_url = 'https://www-net.scc.kit.edu/api'
major_version = 1
minor_version = 0
system_name = 'dns'
objtype_name = 'record'
function_name = 'create'

dbnt_lname = 'dflt:0100'
dbnt_rname = ':'
address_type = '0'
l_uniqueness = '0'
r_uniqueness = '2'
address_type_dict = {
  4: 'A',
  6: 'AAAA'
}

# e.g. 'https://www-net.scc.kit.edu/api/1.0/dns/record/create'
url_mask = '{ru}/{mav}.{miv}/{sys}/{ot}/{func}'
# e.g. 'dflt:0100,:,602,AAAA'
dbrt_mask = '{lnt},{rnt},{at}{lunq}{runq},{rrt}'

url = url_mask.format(
  ru   = root_url,
  mav  = major_version,
  miv  = minor_version,
  sys  = system_name,
  ot   = objtype_name,
  func = function_name
)

# list of tuples with host data to be loaded
# the first uses 'ip dual stack'
rr_tuple_list = [
  ('host1.scc.kit.edu', '129.13.1.1'),
  ('host1.scc.kit.edu', '2a00:1398:1:1::129.13.1.1'),
  ('host2.scc.kit.edu', '129.13.1.2'),
  ('host3.scc.kit.edu', '129.13.1.3'),
  ('host-a0.scc.kit.edu', '2a00:1398:1:1::a0'),
  ('host-b0.scc.kit.edu', '2a00:1398:1:1::b0'),
]

json_list = list()
for rr_tup in rr_tuple_list:
  (rr_host, rr_addr) = rr_tup
  ipaddr_obj = ipaddress.ip_address(rr_addr)
  address_type = ipaddr_obj.version
  rr_dbrt = dbrt_mask.format(
    lnt = dbnt_lname,
    rnt = dbnt_rname,
    at = address_type,
    lunq = l_uniqueness,
    runq = r_uniqueness,
    rrt = address_type_dict[address_type]
  )
  data_param_list = [
    { "name": "fqdn",    "new_value": rr_host },
    { "name": "inttype", "new_value": rr_dbrt },
    { "name": "data",    "new_value": rr_addr }
  ]
  json_list.append( { "param_list": data_param_list } )

json_string = json.dumps(json_list, ensure_ascii = False)
headers_dict = { "Content-Type": "application/json" }

response = requests.post(url = url, data = json_string, headers = headers_dict, cert = certfilename)
if response.ok:
  # continue with normal request (process result)
  result = response.json()
else:
  # errormessage for further processing
  # get as dict-type:
  err_dict = response.json()
  # get as string-type:
  err_text = response.text
  # only simple print out
  print(err_text)
  # recommended: raise exception
  raise requests.exceptions.RequestException(response.status_code, response.text)

DNS: FQDN

POST-Request

  • Domainänderung mehrerer FQDN’s, ebenso Aktualisierung der FQDN-Info
  • Python-Codevorlage:
#!/usr/bin/python

import requests
import json

certfilename = '/home/username/.ssl/mycert.pem'
root_url = 'https://www-net.scc.kit.edu/api'
major_version = 1
minor_version = 0
system_name = 'dns'
objtype_name = 'fqdn'
function_name = 'update'

# e.g. 'https://www-net.scc.kit.edu/api/1.0/dns/fqdn/update'
url_mask = '{ru}/{mav}.{miv}/{sys}/{ot}/{func}'

url = url_mask.format(
  ru   = root_url,
  mav  = major_version,
  miv  = minor_version,
  sys  = system_name,
  ot   = objtype_name,
  func = function_name
)

# list of tuples with fqdn data to be loaded
fqdn_tuple_list = [
  # (old_fqdn, new_domain, new_fqdn_description)
  ('host1.olddomain.kit.edu',         'newdomain.kit.edu',              'Text fuer host1'),
  ('host2.another-olddomain.kit.edu', 'newdomain.kit.edu',              'sonstiges'),
  ('host3.olddomain.kit.edu',         'another-newdomain.kit.edu',      None),
]

json_list = list()
fqdn_separator = '.'
for fqdn_tup in fqdn_tuple_list:
  (old_fqdn, new_parent_fqdn, new_fqdn_descr) = fqdn_tup
  old_fqdn_label, old_fqdn_domain = old_fqdn.split(fqdn_separator, 1)
  new_fqdn = fqdn_separator.join([old_fqdn_label, new_parent_fqdn])
  data_param_list = [
    { "name": "value",       "old_value": old_fqdn, "new_value": new_fqdn },
    { "name": "description",                        "new_value": new_fqdn_descr }
  ]
  json_list.append( { "param_list": data_param_list } )

json_string = json.dumps(json_list, ensure_ascii = False)
headers_dict = { "Content-Type": "application/json" }

response = requests.post(url = url, data = json_string, headers = headers_dict, cert = certfilename)
if response.ok:
  # continue with normal request (process result)
  result = response.json()
else:
  # errormessage for further processing
  # get as dict-type:
  err_dict = response.json()
  # get as string-type:
  err_text = response.text
  # only simple print out
  print(err_text)
  # recommended: raise exception
  raise requests.exceptions.RequestException(response.status_code, response.text)