2019独角兽企业重金招聘Python工程师标准>>>
测试代码如下:
# -*- coding: UTF-8 -*-from gevent.monkey import patch_all;patch_all()
from multiprocessing import Process,cpu_count
import urllib2,gevent,random,sys
import COOKIElib
def start():url = "这里填写你的网站"user_agent = ["Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36","Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13","Mozilla/5.0 (iPhone; U; Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3","Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13 ","Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12"]while 1:try:User_Agent = random.choice(user_agent)COOKIE = COOKIElib.COOKIEJar()opener = urllib2.build_opener(urllib2.HTTPCOOKIEProcessor(COOKIE))request = urllib2.Request(url)request.add_header('User-Agent',User_Agent)opener.open(request)except IOError:#print "unknow error"opener.close()finally:opener.close()COOKIE.clear()def main():gevent.joinall([gevent.spawn(start) for i in range(1000)])if __name__ == '__main__':try:for i in range(cpu_count()):#充分利用多核心p = Process(target=main)p.start()except:sys.exit(0)
模拟CPU数量的并发,保证充足的CPU利用率!