Skip to content

Instantly share code, notes, and snippets.

@evilabandon
Created April 11, 2013 03:51
Show Gist options
  • Save evilabandon/5360605 to your computer and use it in GitHub Desktop.
Save evilabandon/5360605 to your computer and use it in GitHub Desktop.
python multiprocessing
#!/home/yinyong/app/python/bin/python
# -*- coding=utf-8 -*-
#module
import multiprocessing
import urllib2
import json
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#Variable
url="http://ip.taobao.com/service/getIpInfo.php?ip="
#function
def getiplist(ipfile):
f = open(ipfile,'r')
iplist = f.readlines()
f.close
return iplist
def ip_detail(iplist):
for ip in iplist:
newip = ip.strip('\n')
try:
uri = url+newip
request = urllib2.Request(uri)
response = urllib2.urlopen(request,timeout=2)
data = response.read()
datadict = json.loads(data)
#return u"该IP的详细信息为:\n" + u"国家/地区:" + datadict["data"]["country"] + "/" + datadict["data"]["area"] + u"\n省份:\t" + datadict["data"]["region"] + u"\n城市:\t" + datadict["data"]["city"] + u"\n运营商:" + datadict["data"]["isp"]
result = newip + '\t' + datadict["data"]["country"] + "/" + datadict["data"]["area"] + "\t" + datadict["data"]["region"] + "/" + datadict["data"]["city"] + "\t" + datadict["data"]["isp"]
sys.stdout.write(unicode(result) + '\n')
#RES.append(result)
except:
continue
iplist = getiplist(sys.argv[1])
leniplist = len(iplist)
procs_num = 100
procs = []
per_proc = leniplist/procs_num
for i in range(procs_num):
if procs_num-i == 1:
p = multiprocessing.Process(target=ip_detail,args=(iplist[i*per_proc:],))
else:
p = multiprocessing.Process(target=ip_detail,args=(iplist[i*per_proc:i*per_proc+per_proc],))
procs.append(p)
for i in range(procs_num):
procs[i].start()
for i in range(procs_num):
procs[i].join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment