So cute are you Python 11

1.python 实现路由探测

代码

#!/usr/bin/evn python
#coding:utf-8
#FileName:routertracert.py
#Function show the path of an website
import httplib2
import sys
 
if len(sys.argv) < 3:
    print("Written by y...\r\n")
    print("Usage:"+sys.argv[ 0]+" host "+"  .ext")
    print ("Eg:"+sys.argv[ 0]+" http://www.baidu.com .php")
    sys.exit( 0)
 
host= sys.argv[ 1]
ext=sys.argv[ 2]
print host
 
http=httplib2.Http(".cache")
response,content =http.request(host)
 
response,content =http.request(host+"/mustnotexistspath")
nonpathstatus=response.status
 
response,content=http.request(host+'/mustnotexistspath'+ext)
nonpathextstatus = response.status
 
print ("NoneExistFileStatus:",nonpathstatus)
print ("NoneExistFileStatus:",nonpathextstatus)
 
f =open('WebPath.txt','r')
fileList = f.readlines()
 
def subscan(subpath):
    for fileline in fileList:
        newline=fileline.strip()
        path =subpath+"/"+newline
        response,content= http.request(path,'GET')
         
        if response.status!=nonpathstatus:
            st = str(response.status)
            print(st+" : "+path)
            subscan(path)
 
        pathext=path+ext
        response,content= http.request(pathext,'GET')
        if response.status!=nonpathextstatus:
            st = str(response.status)
            print(st+" : "+pathext)
 
subscan(host)
f.close()


说明 NoneExistFileStatus: 200 说明资源可能存在 403 404 资源不可达或禁止访问

2.条件线程实现

#!/usr/bin/evn python
#coding:utf-8
#FileName:thread_judge.py
#function:threading with more choices
#History:22-10-2013
import threading,time
class Producer(threading.Thread):
    def __init__(self,t_name):
        threading.Thread.__init__(self,name=t_name)
 
    def run(self):
        global x
        con.acquire()
 
        if x> 0:
            print 'At least have something to use.'
            con.wait()
        else:
            for i in range( 5):
                x=x+ 1
                print "Producing..."+str(x)
            con.notify()
        print x
        con.release()
 
class Consumer(threading.Thread):
    def __init__(self,t_name):
        threading.Thread.__init__(self,name=t_name)
 
    def run(self):
        global x
        con.acquire()
 
        if x== 0:
            print "consumer wait."
            con.wait()
        else:
            for i in range( 5):
                print "consuming..."+str(x)
                x=x- 1
            con.notify()
        print x
        con.release()
 
 
con =threading.Condition()
x= 0
print 'start consumer \r\n'
c=Consumer('consumer')
print 'start producer\r\n'
p=Producer('producer')
 
p.start()
c.start()
p.join()
c.join()
 
print 'Then end of consumer...'
3.线程池的简单实验一:

#!/usr/bin/evn python
#coding:utf-8
#FileName:thread_query.py
#function: Python中的Queue对象也提供了对线程同步的支持
#History:22-10-2013
from Queue import Queue
import random
import threading
import time
 
#Producer thread
class Producer(threading.Thread):
    def __init__(self,t_name,queue):
        threading.Thread.__init__(self,name=t_name)
        self.data=queue
 
    def run(self):
        for i in range( 5):
            print '%s: %s is producing %d to the query!\n'%(time.ctime(),self.getName(),i)
            time.sleep(random.randrange(10)/ 5)
            self.data.put(i)
        print '%s: %s finished!\n'%(time.ctime(),self.getName())
 
#Consumer thread
class Consumer(threading.Thread):
    def __init__(self,t_name,queue):
        threading.Thread.__init__(self,name=t_name)
        self.data=queue
 
    def run(self):
        for i in range( 5):
            val = self.data.get()
            print '%s: %s is consuming. %d in the query is consumered!\n'%(time.ctime(),self.getName(),val)
            time.sleep(random.randrange(10))
        print "%s: %s finished!" %(time.ctime(),self.getName())
 
def main():
    queue=Queue()
    producer=Producer('Pro.',queue)
    consumer=Consumer('Con.',queue)
 
    producer.start()
    consumer.start()
 
    producer.join()
    consumer.join()
 
    print 'All threads terminate!\r\n'
 
if __name__ == '__main__':
    main()
         


4.线程池实验 二:

#!/usr/bin/evn python
#coding:utf-8
#FileName:thread_pool.py
#function: Python中的Queue testing more of it
#History:22-10-2013
from Queue import Queue
from threading import Thread
 
class Worker(Thread):
    def __init__(self,tasks):
        Thread.__init__(self)
        self.tasks=tasks
        self.daemon=True
        self.start()
 
    def run(self):
        while True:
            func,args,kargs=self.tasks.get()
            try:
                func(*args,**kargs)
            except Exception,e:
                print e
            self.tasks.task_done()
 
 
class ThreadPool:
     
    def __init__(self,num_threads):
        self.tasks=Queue(num_threads)
        for _ in range(num_threads):
            Worker(self.tasks)
 
    def add_task(self,func,*args,**kargs):
        self.tasks.put((func,args,kargs))
 
    def wait_completion(self):
        self.tasks.join()
 
if __name__=='__main__':
    from random import randrange
    delays =[randrange( 1,10) for I in range(100)]
 
    from time import sleep
    def wait_delay(d):
        print 'Sleeping for (%d)sec'%d
        sleep(d)
 
    pool=ThreadPool(20)
 
    for i,d in enumerate(delays):
        print ' Complete:%.2f %c '%((float(i)/float(len(delays)))*100.0,'%')
        pool.add_task(wait_delay,d)
 
    pool.wait_completion()
 
    print 'DONE !!!\r\n'





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值