Python查詢IP地址歸屬完整代碼
本文實(shí)例為大家分享了Python查詢IP地址歸屬的具體代碼,供大家參考,具體內(nèi)容如下
#!/usr/bin/env python # -*- coding: utf-8 -*- #查找IP地址歸屬地 #writer by keery_log #Create time:2013-10-30 #Last update:2013-10-30 #用法: python chk_ip.py www.google.com |python chk_ip.py 8.8.8.8 |python chk_ip.py ip.txt import signal import urllib import json import sys,os,re import socket if len(sys.argv) <= 1 : print "Please input ip address !" sys.exit(0) def handler(signum, frame): sys.exit(0) signal.signal(signal.SIGINT, handler) url = "http://ip.taobao.com/service/getIpInfo.php?ip=" #查找IP地址 def ip_location(ip): data = urllib.urlopen(url + ip).read() datadict=json.loads(data) for oneinfo in datadict: if "code" == oneinfo: if datadict[oneinfo] == 0: return datadict["data"]["country"] + datadict["data"]["region"] + datadict["data"]["city"] + datadict["data"]["isp"] #定義IP與域名正則 re_ipaddress = re.compile(r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$') re_domain = re.compile(r'[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?') if os.path.isfile(sys.argv[1]): #如果參數(shù)是文件,迭代查找 file_path = sys.argv[1] fh = open(file_path,'r') for line in fh.readlines(): if re_ipaddress.match(line): city_address = ip_location(line) print line.strip() + ":" + city_address else: ip_address = sys.argv[1] if re_ipaddress.match(ip_address): #如果參數(shù)是單個(gè)IP地址 city_address = ip_location(ip_address) print ip_address + ":" + city_address elif(re_domain.match(ip_address)): #如果參數(shù)是域名 result = socket.getaddrinfo(ip_address, None) ip_address = result[0][4][0] city_address = ip_location(ip_address) print ip_address.strip() + ":" + city_address
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
linux上運(yùn)行python腳本,SyntaxError:?invalid?syntax的解決
這篇文章主要介紹了linux上運(yùn)行python腳本,SyntaxError:?invalid?syntax的解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Python裝飾器類方法擴(kuò)展元類管理實(shí)例探究
這篇文章主要為大家介紹了Python裝飾器類方法擴(kuò)展元類管理實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Pandas 數(shù)據(jù)處理,數(shù)據(jù)清洗詳解
今天小編就為大家分享一篇Pandas 數(shù)據(jù)處理,數(shù)據(jù)清洗詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07使用Tensorflow實(shí)現(xiàn)可視化中間層和卷積層
今天小編就為大家分享一篇使用Tensorflow實(shí)現(xiàn)可視化中間層和卷積層,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01通過Python腳本批量復(fù)制并規(guī)范命名視頻文件
本文介紹了如何通過Python腳本批量復(fù)制并規(guī)范命名視頻文件,實(shí)現(xiàn)自動(dòng)補(bǔ)齊數(shù)字編號(hào)、保留原始文件、智能識(shí)別有效文件等功能,聽過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)感知器算法
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)感知器算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Python?GUI實(shí)現(xiàn)PDF轉(zhuǎn)Word功能
這篇文章主要介紹了如何使用?wxPython?創(chuàng)建一個(gè)簡單的圖形用戶界面(GUI)應(yīng)用程序,結(jié)合?pdf2docx?庫,實(shí)現(xiàn)將?PDF?轉(zhuǎn)換為?Word?文檔的功能,需要的可以參考下2024-12-12Python中列表、字典、元組數(shù)據(jù)結(jié)構(gòu)的簡單學(xué)習(xí)筆記
這篇文章主要介紹了Python中列表、字典、元組數(shù)據(jù)結(jié)構(gòu)的簡單學(xué)習(xí)筆記,文中講到了字典在Python3中特性和操作方法的一些變化,需要的朋友可以參考下2016-03-03python結(jié)合opencv實(shí)現(xiàn)人臉檢測與跟蹤
在Python下用起來OpenCV很爽,代碼很簡潔,很清晰易懂。使用的是Haar特征的分類器,訓(xùn)練之后得到的數(shù)據(jù)存在一個(gè)xml中。下面我們就來詳細(xì)談?wù)劇?/div> 2015-06-06最新評論