Python多線程自動刷票腳本

#!/usr/bin/python  
# -*- coding: utf-8 -*-  
#coding=utf-8
import urllib2
import urllib
import re
import threading
from time import ctime
rlock = threading.RLock()
#myproxy代理地址
# i線程id
def vote(myproxy,i):
	try:
		print "voting...%d..." % i
		#採用代理ip發送數據
		proxy_support = urllib2.ProxyHandler(myproxy)
		opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
		urllib2.install_opener(opener)

		sendt = '投票'.decode('utf-8').encode('gb2312')
		#發送數據目標地址
		url = '' 
		#post數據
		values = {}  
		req = urllib2.Request(url,urllib.urlencode(values)) 
		response = opener.open(req) 
		html = response.read() 
		#print html
		if html.find('投票人數過多'.decode('utf-8').encode('gb2312')):
			print "VOTE %d Faild Plase wait a minute" % i
			return False
		else:
			print "VOTE %d Success" % i
			return Ture
	except Exception:
		print " Error The %d proxy cann't do anything" % i
		return False

if __name__ == "__main__":
	#讀取ip.txt文件 ip格式:1.179.128.2:8080@HTTP#泰國
	ipFile = open('ip.txt')
	ipList = ipFile.readlines()
	ipFile.close()

	length = range(len(ipList))
	#print length
	threads = []
	for i in length:
		ipLine = ipList[i]
		stringList = ipLine.split('@')
		ip=stringList[0]
		myproxy = {'http': ip}
		#print myproxy
		t = threading.Thread(target=vote,args=(myproxy,i))
		threads.append(t)
	for i in length:
		threads[i].start()

	for i in length:
		threads[i].join()
	
	print "all done at", ctime()
	
		

轉自:http://blog.csdn.net/cnairng/article/details/11924169


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章