分类: 系统运维
首先是一个初始多线程以及设置代理地址的地方,我称他为kk2.py
import urllib2,cookielib
import urllib, time, string
import threading
####创建线程#
class PrintThread(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self)
self.sleepTime = (1)
self.name = threadname
####创建线程任务 ###
def run(self):
App().runnum() #任务是执行下面的读页面操作
class App(PrintThread):
def __init__(self):
self.cookie=cookielib.CookieJar()
self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
urllib2.install_opener(self.opener)
#设置代理服务器地址,网上大把,下面的不一定能用
self.proxy = ['http://125.244.164.69:8080','http://125.245.226.2:8080','http://125.248.244.131:8080',
'http://125.250.30.82:8081',
'http://125.31.236.243:3128',
'http://125.64.33.25:80',
'http://125.90.64.72:80',
'http://125.90.64.74:80',
'http://142.red-213-37-2.user.auna.net:8080',
'http://163.28.33.226:3128',
'http://163.28.33.226:8088',
'http://194.204.111.94:3128',
'http://195.175.37.6:8080',
'http://195.175.37.70:80',
'http://195.175.37.70:8080',
'http://195.175.37.71:80']
def runnum(self):
### 用for来循环使用代理服务器
for self.x in range(len(self.proxy)):
self.MYPROXY={'http':self.proxy[self.x]}
print self.MYPROXY
### 真正开始利用代理读页面
try:
self.login_response= urllib.urlopen(r'http://xy2.163.com/php/trueshow/vote.php?id=85614',proxies = self.MYPROXY) #
ll=self.login_response.read()
except IOError: #如果代理服务器不可用再跳至下一个代理地址继续读页面
print "Error,The proxy cann't to do anything,try next... "
self.x =+1
if __name__=='__main__':
App().runnum()
下面是真正运行并指定连接数的文件,我叫他121y.py
import urllib2,cookielib
import urllib, time, string
import datetime
## 启动多线程任务
while True:
import kk2
for x in range(1,100):
t1=kk2.PrintThread(str(x))
print str(x)+':::', datetime.datetime.now()
t1.start()
在这里设置了多线程共同操作的内容以及提示,写完收功,启动程序看到你刷新的地址的浏览量每10秒成百上千的加