V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lenovo
V2EX  ›  Google

有熟悉 google maps api 的吗?

  •  
  •   lenovo · 2015-01-26 08:20:01 +08:00 · 2091 次点击
    这是一个创建于 3594 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    ImageUS
    ImageUK

    #!/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()
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3234 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:18 · PVG 20:18 · LAX 04:18 · JFK 07:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.