| | 72 | |
| | 73 | == HostIP を使う == |
| | 74 | {{{ |
| | 75 | #!python |
| | 76 | import urllib |
| | 77 | import mimetools |
| | 78 | |
| | 79 | class HostIP(dict): |
| | 80 | _URL_BASE = 'http://api.hostip.info/rough.php?position=true&ip=%s' |
| | 81 | _GOOGLEMAPS_BASE = 'http://maps.google.com/?q=%sN+%sE(%s)' |
| | 82 | |
| | 83 | def __init__(self, ipaddr): |
| | 84 | url = self._URL_BASE % ipaddr |
| | 85 | |
| | 86 | fp = urllib.urlopen(url) |
| | 87 | headers = mimetools.Message(fp, 0) |
| | 88 | fp.close() |
| | 89 | |
| | 90 | dict.__init__(self, headers.dict) |
| | 91 | self['ipaddr'] = ipaddr |
| | 92 | self['url'] = url |
| | 93 | self['googlemaps'] = self._GOOGLEMAPS_BASE % (self['latitude'], self['longitude'], ipaddr) |
| | 94 | }}} |
| | 95 | |
| | 96 | * dict として使える HostIP オブジェクト. |