#! /usr/bin/env python
# -*- coding: utf-8 -*-
import redis
import urllib2
class RedisConnect:
#class member
__redisId=""
__host=""
__port=""
__db=""
__pwd=""
#Add an element to the collection
def setSadd(self,key,value):
try:
return self.__redisId.sadd(key,value)
except Exception,e:
print e
#Delete an element in a collection.
def srem(self,key,member):
try:
return self.__redisId.srem(key,member)
except Exception,e:
print e
#Get the total number of elements in the collection
def getScard(self,key):
try:
return self.__redisId.scard(key)
except Exception,e:
print e
#Get some member
def getSrandmember(self,key,count):
try:
return self.__redisId.srandmember(key,count)
except Exception,e:
print e
#To determine whether the set in the member
def getSismember(self,key,member):
try:
return self.__redisId.sismember(key,member)
except Exception,e:
print e
#Delete all keys
def delAllKey(self):
try:
return self.__redisId.flushdb()
except Exception,e:
print e
def getIp(count):
url = "http://xvre.daili666api.com/ip/?tid=xxxxxxxxxxx&num=%d"%(count)
req = urllib2.Request(url)
res_data = urllib2.urlopen(req)
res = res_data.read().split("\r\n")
return res
def main():
try:
redis=RedisConnect("xxxxxxxxxx",0000,x,"xxxxxxxxx")
count=redis.getScard("ipPool")
if not count:
Ips=getIp(10000)
for i in Ips:
print "------Is insert ip :%s---------"%i
redis.setSadd("ipPool",i)
if redis.getScard("ipPool")>=1000:
break
else:
ipList=redis.getSrandmember("ipPool",500)
for i in ipList:
print "xxxxxxxxxxIs delete ip :%sxxxxxxxxxxx"%i
redis.srem("ipPool",i)
Ipss=getIp(10000)
m=0
while redis.getScard("ipPool") <1000:
print redis.getScard("ipPool")
print "------Is update ip :%s---------"%Ipss[m]
redis.setSadd("ipPool",Ipss[m])
m+=1
print "--------------------------Update Ip Success----------------------------"
except Exception,e:
print e
#Delete some ip
if __name__ == "__main__":
main()