淘寶接口-IP返回運營商


#!/usr/bin/evn python
# -*- coding:utf-8 -*-
import json
import urllib2
import datetime
import re
import Queue
import threading
queue = Queue.Queue()
thread = 3
import sys
reload(sys)
sys.setdefaultencoding("utf-8")


days = days = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y%m%d')
rex = re.compile('OpenPlatfrom')


def writefile(content):
    f = open('check.log','a+')
    f.write(content + '\n')
    f.close()

def Getip_isp(i, q):
    while True:
        ip = q.get()
        url = 'http://ip.taobao.com/service/getIpInfo.php?ip=%s' % ip
        try:
            s = json.loads(urllib2.urlopen(url, timeout=2).readlines()[0])
            s = str(s['data']['isp']) + ',' + str(ip) + ',' + str(ip_dic.get(ip))
        except:
            s = "timeout"
        writefile(s)
        q.task_done()


def readfile():
    isp_dic = {}
    logfile = "/home/log/weblog/elk-web-%s.log" % days
    f = open(logfile)
    for line in f:
        if rex.search(line):
            new = json.loads(json.loads(line.strip())['extra'])
            try:
                isp_dic[new['clientIp']] = new['condIp']
            except:
                pass
    f.close()
    return isp_dic
ip_dic = readfile()
print len(ip_dic)
for k, v in ip_dic.items():
    queue.put(k)
    #isp = Getip_isp(k)
    #print k, isp, v

for i in range(thread):
    run = threading.Thread(target=Getip_isp, args=(i, queue))
    run.setDaemon(True)
    run.start()
#run.join()
queue.join()


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: