|
1 | | -import requests |
2 | | -import json |
3 | | -import ast |
4 | | -import pandas as pd |
5 | | -import time |
| 1 | +from IPGeoSearch import search |
6 | 2 |
|
7 | | -def flatten_json(nested_json): |
8 | | - """ |
9 | | - Flatten json object with nested keys into a single level. |
10 | | - Args: |
11 | | - nested_json: A nested json object. |
12 | | - Returns: |
13 | | - The flattened json object if successful, None otherwise. |
14 | | - """ |
15 | | - out = {} |
16 | | - |
17 | | - def flatten(x, name=''): |
18 | | - if type(x) is dict: |
19 | | - for a in x: |
20 | | - flatten(x[a], name + a + '_') |
21 | | - elif type(x) is list: |
22 | | - i = 0 |
23 | | - for a in x: |
24 | | - flatten(a, name + str(i) + '_') |
25 | | - i += 1 |
26 | | - else: |
27 | | - out[name[:-1]] = x |
28 | | - |
29 | | - flatten(nested_json) |
30 | | - return out |
31 | | -#Sets Post URL |
32 | | -url = 'https://ipgeo.azurewebsites.net/try' |
33 | | - |
34 | | -#opening list of IP's |
35 | 3 | with open('ipList.txt', 'r') as f: |
36 | | - ipList = [line.strip() for line in f] |
| 4 | + ips = [line.strip() for line in f] |
37 | 5 | f.close() |
38 | | -#checking if IP List is compatible with version |
39 | | -if len(ipList) > 10: |
40 | | - print("Your IP List is longer than 10 entires, which is more than alloted for your version. Sending it would result in an error from the server.") |
41 | | - print("Please shorten your list so that all your IP's may be processed.") |
42 | | - time.sleep(5) |
43 | | - exit() |
44 | | - |
45 | | -#Recursively sending requests to the server |
46 | | -for ip in ipList: |
47 | | - ipsearch = "{\n\t\"ip\":\""+ip+"\"\n}" |
48 | | - authentication = {'Content-Type': "application/json"} |
49 | | - res = requests.post(url, data=ipsearch, headers=authentication) |
50 | | - res = res.text |
51 | | - with open("results/json/result-ip-"+ip+".json", 'w') as result: |
52 | | - result.write(res) |
53 | | - result.close() |
54 | | - with open("results/json/result-ip-"+ip+".json", 'r') as result: |
55 | | - data = json.load(result) |
56 | | - data = flatten_json(data) |
57 | | - ipAddress = [""+ip+""] |
58 | | - df = pd.Series(data).to_frame().T |
59 | | - df['ip'] = ipAddress |
60 | | - #processing and removing unnecessary fields from the result |
61 | | - if set(['city_names_en','subdivisions_0_names_en']).issubset(df.columns): |
62 | | - df = df[['ip','city_names_en','subdivisions_0_names_en','country_names_en','continent_names_en','location_latitude','location_longitude', |
63 | | - 'autonomous_system_number','autonomous_system_organization','isp','organization','organization_type','isic_code','naics_code','connection_type' |
64 | | - ,'ip_routing_type', 'line_speed']] |
65 | | - df = df.rename(columns={'city_names_en':'City','subdivisions_0_names_en':'State/Province','country_names_en':'Country','continent_names_en':'Continent', |
66 | | - 'location_latitude':'Latitude','location_longitude':'Longitude','autonomous_system_number':'ASN','autonomous_system_organization':'ASO', |
67 | | - 'isp':'ISP', 'organization':'Organization','organization_type':'Organization Type','isic_code':'ISIC','naics_code':'NAICS' |
68 | | - ,'connection_type':'Connection Type','ip_routing_type':'IP Routing Type','line_speed':'Line Speed'}) |
69 | | - elif 'city_names_en' in df: |
70 | | - df = df[['ip','city_names_en','country_names_en','continent_names_en','location_latitude','location_longitude', |
71 | | - 'autonomous_system_number','autonomous_system_organization','isp','organization','organization_type','isic_code','naics_code','connection_type', |
72 | | - 'ip_routing_type','line_speed']] |
73 | | - df = df.rename(columns={'city_names_en':'City','country_names_en':'Country','continent_names_en':'Continent', |
74 | | - 'location_latitude':'Latitude','location_longitude':'Longitude','autonomous_system_number':'ASN','autonomous_system_organization':'ASO', |
75 | | - 'isp':'ISP', 'organization':'Organization','organization_type':'Organization Type','isic_code':'ISIC','naics_code':'NAICS' |
76 | | - ,'connection_type':'Connection Type','ip_routing_type':'IP Routing Type','line_speed':'Line Speed'}) |
77 | | - elif 'subdivisions_0_names_en' in df: |
78 | | - df = df[['ip','subdivisions_0_names_en','country_names_en','continent_names_en','location_latitude','location_longitude', |
79 | | - 'autonomous_system_number','autonomous_system_organization','isp','organization','organization_type','isic_code','naics_code','connection_type', |
80 | | - 'ip_routing_type','line_speed']] |
81 | | - df = df.rename(columns={'subdivisions_0_names_en':'State/Province','country_names_en':'Country','continent_names_en':'Continent', |
82 | | - 'location_latitude':'Latitude','location_longitude':'Longitude','autonomous_system_number':'ASN','autonomous_system_organization':'ASO', |
83 | | - 'isp':'ISP', 'organization':'Organization','organization_type':'Organization Type','isic_code':'ISIC','naics_code':'NAICS' |
84 | | - ,'connection_type':'Connection Type','ip_routing_type':'IP Routing Type','line_speed':'Line Speed'}) |
85 | | - else: |
86 | | - df = df[['ip','country_names_en','continent_names_en','location_latitude','location_longitude', |
87 | | - 'autonomous_system_number','autonomous_system_organization','isp','organization','organization_type','isic_code','naics_code','connection_type', |
88 | | - 'ip_routing_type','line_speed']] |
89 | | - df = df.rename(columns={'country_names_en':'Country','continent_names_en':'Continent', |
90 | | - 'location_latitude':'Latitude','location_longitude':'Longitude','autonomous_system_number':'ASN','autonomous_system_organization':'ASO', |
91 | | - 'isp':'ISP', 'organization':'Organization','organization_type':'Organization Type','isic_code':'ISIC','naics_code':'NAICS' |
92 | | - ,'connection_type':'Connection Type','ip_routing_type':'IP Routing Type','line_speed':'Line Speed'}) |
93 | | - df.to_csv(path_or_buf="results/csv/result-ip-"+ip+".csv", sep =',', index = False) |
94 | | - result.close() |
95 | 6 |
|
| 7 | +search.search(ipList=ips,path='results/') |
0 commit comments