Find client's location from IP
Use geoip
pip install python-geoip
pip install python-geoip-geolite2
Then your code will look something like this.
from geoip import geolite2
match = geolite2.lookup('8.8.8.8')
print match.location
This produces,
(37.386, -122.0838)
Find locations of all AWS centers
The information is available from: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/LocationsOfEdgeServers.html you need to find the geolocations for them. That can be done with geopy
pip install geopy
Then
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("Singapore")
print location.latitude, location.longitude
Which gives
1.2904527 103.852038
You need to do this for all your locations and save the data somehwere. Possibly in an RDBMS (if you are doing that consider using django, django has excellent support for geolocation searching using GeoDjango)
Finally finding the distance
Having found the client location, let us call it l1, and having found the data center locations, it's time to find the distance
from geopy.distance import great_circle
great_circle(l1.point, l2.point)
And there you have the distance
Finding the closest distance
You could loop through all your saved locations and find the closest distance, or if you saved your data in an RDBMS that supports geospatial data (postgis immidiately comes to mind) you can use the ST_Distance
function to do the distance compaison quickly and effectively with very little code. As mentioned earlier, django has excellent support for geospatial queries.
If you were to use Postgis/Django , the loop involving great_circle would be replaced by a call to st_distance.