国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實(shí)現(xiàn)ip查詢示例

2020-02-23 05:16:33
字體:
供稿:網(wǎng)友

以下代碼實(shí)現(xiàn)了ip查詢功能

處理程序
代碼如下:
import os,time

def getip(filepath):
    ip2city={}
    file=open(filepath,'r')
    lines=file.readlines()
    file.close()
    for line in lines:
        ip=line.split(' ')[0]
        city=line.split(' ')[1]
        haship=hashm(ip)
        if haship in ip2city:
            pass
        else:
            ip2city[haship]=city
    print('Hash done!')
    return ip2city

def hashm(ip):
    iplist=ip.split('.')
    ip=int(iplist[0])*4+int(iplist[1])*2+int(iplist[2])
    return ip

def getcityfromip(filepath,ipandcity):
    outputstr=[]
    for file in os.listdir(filepath):
        file_handler=open(filepath+'//'+file,'r')
        line=file_handler.readline()
        while line:
            ip=hashm(line.rstrip())
            if ip in ipandcity:
                outputstr.append(line.rstrip()+'    '+ipandcity[ip])
            line=file_handler.readline()
        file_handler.close()
        outfile_handler=open(filepath+'//'+file.split('.')[0]+'_out.txt','a+')
        outfile_handler.writelines(outputstr)
        outfile_handler.close()
        print(file.split('.')[0]+'_out.txt'+'done!')
       

def splitfile(filepath):
    file=open(filepath,'r')
    block_size=8000000
    filecount=1
    temp=[]
    count=0
    line=file.readline()
    while line or temp:
        if count==block_size:
            wfile=open('D://ipfile//file_'+str(filecount)+'.txt','a+')
            wfile.writelines(temp)
            temp=[]
            count=0

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 吴旗县| 隆安县| 景泰县| 托里县| 内黄县| 英山县| 柯坪县| 泗洪县| 义乌市| 石狮市| 固始县| 福建省| 万安县| 襄垣县| 临沭县| 南昌县| 台南市| 府谷县| 双峰县| 延吉市| 安宁市| 来安县| 道孚县| 农安县| 确山县| 邢台县| 盐城市| 雅安市| 营山县| 广丰县| 洞头县| 宁蒗| 改则县| 裕民县| 开阳县| 邳州市| 苍梧县| 太仓市| 黄石市| 诸暨市| 玛纳斯县|