Skip to content

Commit 40f2d75

Browse files
Added use of IPGeoSearch Module
1 parent d466c39 commit 40f2d75

4 files changed

Lines changed: 29 additions & 105 deletions

File tree

README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
# IPGeo
22

3+
## About IPGeo
4+
5+
The IPGeo repository on GitHub is a free version of the IP geolocation Module found on [Our Webpage](http://ipgeo.azurewebsites.net/).
6+
7+
Paid versions are described more in detail on [Our Webpage](http://ipgeo.azurewebsites.net/).
8+
9+
Our Free Version Includes 10 searches a month. Exceeding the 10 Searches a month will lead to an error as the server will not send back the IP Search Data.
10+
11+
Feel Free to check out the [IPGeoSearch](https://github.com/MatthiasRathbun/IPGeo-Search) library on GitHub for more ways to use our API!
12+
313
## Requirements
414

5-
Before running on your local computer, make sure you have ```python 3.6+``` with the latest version of pandas installed and ```ipList.txt``` in the same folder as the python file.
15+
Before running on your local computer, make sure you have `python 3.6+` with the latest version of `pandas` and `IPGeoSearch` installed and `ipList.txt` in the same folder as the python file.
16+
17+
To Install Pandas, run
18+
19+
```cmd
20+
pip install pandas
21+
```
22+
23+
To Install IPGeoSearch, run
624

7-
To Install Pandas, run ```pip install pandas```.
25+
```cmd
26+
pip install IPGeoSearch
27+
```
828

929
Request.txt should be formatted in one IP address per line like such:
1030

@@ -18,19 +38,19 @@ Request.txt should be formatted in one IP address per line like such:
1838

1939
## How To Use
2040

21-
### Python
41+
### Python Example
2242

23-
The ```results``` folder will have all the ```.json``` and ```.csv``` files stored in it after running. ```.json``` files are found in ```results/json/``` while the ```.csv``` files are found in ```results/csv/```
43+
The `results` folder will have all the `.json` and `.csv` files stored in it after running.
2444

25-
To query IP, run
45+
To query IP using our sample file, run
2646

2747
```cmd
2848
python request.py
2949
```
3050

3151
in your command prompt environment with the pandas installed.
3252

33-
Files named ```result-ip-x.x.x.x.csv``` and ```result.json-x.x.x.x.json``` are saved which have the relevant information regarding your IP Search. Files are named with the IP in the file Name. An example csv for a search of Google's IP ```8.8.8.8``` would be named ```result-ip-8.8.8.8.csv```.
53+
Files named `x.x.x.x.csv` and `x.x.x.x.json` are saved which have the relevant information regarding your IP Search. Files are named with the IP in the file Name. An example csv for a search of Google's IP `8.8.8.8` would be named `8.8.8.8.csv`.
3454

3555
Errors are more than likely caused by a non existent IP in the data base.
3656

@@ -43,11 +63,3 @@ curl https://ipgeo.azurewebsites.net/try -H "Content-type:application/json" -X P
4363
```
4464

4565
Using Curl is not recommended as it will not automatically parse the json into a csv and will not automate requests.
46-
47-
## About IPGeo
48-
49-
The IPGeo repository on GitHub is a free version of the IP geolocation Module found on [Our Webpage](http://ipgeo.azurewebsites.net/).
50-
51-
Paid versions are described more in detail on [Our Webpage](http://ipgeo.azurewebsites.net/).
52-
53-
Our Free Version Includes 10 searches a month. Exceeding the 10 Searches a month will lead to an error as the server will not send back the IP Search Data.

request.py

Lines changed: 3 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,7 @@
1-
import requests
2-
import json
3-
import ast
4-
import pandas as pd
5-
import time
1+
from IPGeoSearch import search
62

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
353
with open('ipList.txt', 'r') as f:
36-
ipList = [line.strip() for line in f]
4+
ips = [line.strip() for line in f]
375
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()
956

7+
search.search(ipList=ips,path='results/')

0 commit comments

Comments
 (0)