有熟悉 google maps api 的吗?

2015-01-26 08:20:01 +08:00
 lenovo

https://kyleflan.wordpress.com/2012/12/13/a-visual-traceroute-program/
把这个生成tracert地图的python程序改了一下,用上了IP2Location的离线数据库,但是到欧洲的时候中间的连线没了,该怎么弄?谢了


#!/usr/bin/env python

# https://kyleflan.wordpress.com/2012/12/13/a-visual-traceroute-program/

# (C) Kyle Flanagan 2012
# vistraceroute
#
# This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#from pyipinfodb import *
import subprocess, urllib, urllib2, sys, os
import IP2Location;
# api key for ipinfodb.com
#API_KEY = "687f07cd5c6a20f0d7a6890751f049a3745c95f98c7706500bfd1fce73f0a1d0"
# URL for Google Static Map API
google_map_base_url = "http://maps.googleapis.com/maps/api/staticmap?"

def getURLDict(ips):
    """
    Returns a dictionary consisting of URL paramenters to pass to the Google
    static map api. The IP addresses in ips are looked up using the IPInfo
    class from pyipinfodb and constructed into the url_dict.
    """
    url_dict = {'zoom' : '', 
            'maptype':'roadmap', 
            'sensor':'false', 
            'size':'640x640', 
            'markers':[], 
            'path':'color:0x0000ff|weight:3',
            'center':'',
            'scale':'2'}
    for x, ip in enumerate(ips):
        IP2LocObjLite = IP2Location.IP2Location();
        IP2LocObjLite.open("IP2LOCATION-LITE-DB11.BIN");
        recLite = IP2LocObjLite.get_all(ip);
        location = recLite.region + ',' + recLite.city
#        ipi = IPInfo(API_KEY, ip, city=True) # get city, state
#        location = ipi.getCity() + ',' + ipi.getRegion()
        print "IP: " + ip + " Location: " + location 

        # the IPInfo class returns '-' if it cannot find a city or
        # state, so ignore these values
        if '-' in location:
            continue

        # we could use the heuristic below to find an approximate center
        # for the map, but since we're passing markers and a path, Google
        # will find the center for us
        ##if len(ips) / 2 <= x and url_dict['center'] == None:
        ##    url_dict['center'] = ipi.getCity() + ',' + ipi.getRegion() 

        # append markers
        if x == len(ips)-1: # end with red marker
            url_dict['markers'].append('color:red|label:' + str(x) + '|' + location)
        else: # else use a green one
            url_dict['markers'].append('color:green|label:' + str(x) + '|' + location)

        # append to the path route
        url_dict['path'] = url_dict['path'] + "|" + location
    return url_dict

def main():
    """
    Usage: vistraceroute ip_address
    ---
    vistraceroute uses data from traceroute to query locations of IP addresses.
    Using these locations, it constructs a Google Static Map URL to plot the
    locations and also draws the path from location to location so that the
    user can see a visual represenation of the traceroute data.
    """
    if len(sys.argv) < 2:
        print "Usage: vistraceroute <ip_address>"
        return
    IP = sys.argv[1]
    print "Visual traceroute to IP: " + IP

    # determine system
    posix_system = True
    traceroute = 'traceroute'
    args = [IP]
    if (os.name != "posix"):
        # assume windows
        posix_system = False
        traceroute = 'tracert'
        args.insert(0, '-d') # for windows traceroute to jsut get IP

    args.insert(0, traceroute)
    # args now looks like: traceroute, [flag,] IP

    # start traceroute
    print "Starting traceroute..."
    process = subprocess.Popen(args, 
            shell=False, 
            stdout=subprocess.PIPE)

    # wait for traceroute to finish
    print "Traceroute running. Please wait..."
    if process.wait() != 0:
        print "Traceroute error. Exiting."
        #print process.communicate()[1]
        return

    # read data from traceroute and split it into lines
    lines = process.communicate()[0].split('\n')
    # print out traceroute data for user
    for line in lines:
        print line

    print "Traceroute finished. Looking up IP addresses. Please wait..."
    # first line is traceroute info output, remove it
    lines.pop(0)
    # and there are extra lines on windows output
    if not posix_system:
        lines.pop(0)
        lines.pop(0)
        lines.pop(0)
        lines.pop()
        lines.pop()
        lines.pop()
    # now get hostname, ip from each line
    ips = []
    for line in lines:
        if line != "":
            # if we didn't get a reply from a gateway, ignore that line in the
            # traceroute output
            if '*' in line:
                continue

            # Split the line and extract the hostname and IP address
            if posix_system:
                split_line = line.split('  ')
                ips.append(split_line[1].split(' ')[1][1:-1])
            else: 
                line = line.lstrip()
                line = line.rstrip()
                split_line = line.split(' ')
                ips.append(split_line[-1])

    print "IP addresses to be looked up:"
    for ip in ips:
        print ip

    url_dict = getURLDict(ips)

    urldata = urllib.urlencode(url_dict, True)
    print "Google Map URL (copy and paste into your web browser):"
    url = google_map_base_url + urldata
    print url

if __name__ == "__main__":
    main()
2041 次点击
所在节点    Google
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/165437

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX