for x in range(thlen):
bookid = books[0][0]
bookname = books[0][1]
booksize = books[0][2]
tname = 't' + str(x)
th = threading.Thread(target=caiji,name=tname,args=(bookid,bookname,booksize))
th.start()
del books[0]
bookdict[bookid] = booksize
tsk.append(th)
#检测空闲线程,当线程闲置时,若待采集列表不为空时,启用该线程进行采集
while 1:
time.sleep(5)
for i in range(len(tsk)):
if not tsk[i].is_alive():
print tsk[i].name + '线程空闲'
if len(books) > 0:
bookid = books[0][0]
bookname = books[0][1]
booksize = books[0][2]
th = threading.Thread(target=caiji,name=tsk[i].name,args=(bookid,bookname,booksize))
th.start()
del books[0]
bookdict[bookid] = booksize
tsk[i] = th
while 1:
try:
time.sleep(10)
print '[' + time.strftime('%H:%M:%S') + ']第' + str(i) +'次检测。'
i = i +1
# urllib进行编码
post_data=urllib.urlencode(data)
check_data=urllib.urlencode(checkdata)
# 发送头信息
headers ={
"Host":"panel.op-net.com",
"Referer":auth_url,
"User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36",
}
# 访问检查页 如果获取到csrf则不需要登陆
reqcheck=urllib2.Request(check_url,check_data,headers)
resultcheck = opener.open(reqcheck)
csrf=re.findall(r'csrf_token" value="([0-9a-z]+)"/>',resultcheck.read(),re.M)
if (len(csrf)>0):#找到csrf,不用再登陆
print "have csrf:" + csrf[0]
else:
print "No csrf,auto login......"
# 登陆获取cookie
req=urllib2.Request(auth_url,post_data,headers)
result = opener.open(req)
print "Login OK,next get csrf."
# 访问检查页 重新获取csrf
reqcheck=urllib2.Request(check_url,check_data,headers)
resultcheck = opener.open(reqcheck)
html = resultcheck.read()
csrf=re.findall(r'csrf_token" value="([0-9a-z]+)"/>',html,re.M)
if (len(csrf)>0):#找到csrf
print "new get csrf:" + csrf[0]
else:
print "no csrf,quit."
continue
if not hk_check_str in html:
print "HK is available,next create it!"
location = "13"
elif not jp_check_str in html:
print "JP is available,next create it!"
location = "14"
else:
print "HK and JP is unavaileable,quit."
continue
# 创建VM数据
create={
'csrf_token':csrf[0],
'plan':'Plan 01',
'vm_id':vmid,
'location':location,
'os':'linux-debian-6.0.6-x86_64-min-gen2-v1',
'hostname':'op.vpskk.com',
'root':'',
}
# urllib进行编码
create_data=urllib.urlencode(create)
reqcreate=urllib2.Request(create_url,create_data,headers)
result = opener.open(reqcreate)
if "The requested location is currently unavailable" in result.read():
print "unavailable...waiting for checking again......"
else:
print "Create VM OK"
break
except:
continue作者: hhwg123 时间: 2017-3-19 13:39
提示: 作者被禁止或删除 内容自动屏蔽