Skip to content

Commit

Permalink
PCC16 YauheniKr (pybites#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
YauheniKr authored and pybites committed Jan 7, 2019
1 parent 5944c6f commit 82a7ced
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 16/YauheniKr/program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from request_db import request_as_prefixes, request_as_neighbor
from tabulate import tabulate


def as_prefixes(AS):
descr = []
route = []
list_prefix_data = request_as_prefixes(AS)
i = 1
for diction in list_prefix_data:
if (diction['name'] == 'route' or diction['name'] == 'route6') and i == 0:
descr.append('')
route.append(diction["value"])
i = 0
elif diction['name'] == 'descr':
descr.append(diction["value"])
i = 1
elif (diction['name'] == 'route' or diction['name'] == 'route6') and i == 1:
route.append(diction["value"])
i = 0
if len(descr) < len(route):
descr.append('')
prefix_tuple = tuple(zip(range(1, len(route)+1), route, descr))
return prefix_tuple


def as_neighbor(AS):
dict_data_neighbor = request_as_neighbor(AS)
return dict_data_neighbor


def main():
columns = ['№', 'Route', 'Description']
AS = input('Please, insert AS number: ')
print()
print(f'AS has {as_neighbor(AS)["left"]} uplink AS and has {as_neighbor(AS)["right"]} downlink clients AS')
print()
print(f'AS has {len(as_prefixes(AS))} public prefixes registered')
print()
print(tabulate(as_prefixes(AS), headers=columns, tablefmt="grid"))


if __name__ == '__main__':
main()
14 changes: 14 additions & 0 deletions 16/YauheniKr/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# RIPE NCC Request Tool

This script print table with all public prefixes that belong particular AS
from AS pool managed by RIPE NCC(European Regional Internet Registries)

# Description

Written script make request to RIPE NCC DataBase(http://rest.db.ripe.net/)
and try to retrieve all public prefixes that belongs to public AS. If request
return response with error script print 'Incorrect AS number' and empty table.
Database request in file request_db.by.
Parsing json output in file program.py

For pretty printing used module named tabulate
21 changes: 21 additions & 0 deletions 16/YauheniKr/request_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import json
import requests


def request_as_prefixes(AS):
prefix_r = requests.get(f'https://rest.db.ripe.net/search.json?inverse-attribute=origin&source=ripe&query-string'
f'=AS{AS}&types=route')
prefix_r.raise_for_status()
data = json.loads(prefix_r.text)
data_list = [row_at for row in data['objects']['object'] for row_at in row['attributes']['attribute']]
common_data_list = [diction for diction in data_list if diction['name'] == 'route' or diction['name'] == 'route6'
or diction['name'] == 'descr']
return common_data_list


def request_as_neighbor(AS):
as_neighbor_r = requests.get(f'https://stat.ripe.net/data/asn-neighbours/data.json?resource={AS}')
as_neighbor_r.raise_for_status()
data = as_neighbor_r.json()
as_neighbor = data['data']['neighbour_counts']
return as_neighbor
2 changes: 2 additions & 0 deletions 16/YauheniKr/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.20.0
tabulate==0.8.2

0 comments on commit 82a7ced

Please sign in to comment.