• python --curl重定向到文件范例


     
    import sys
    import os
    import subprocess
    import time
    
    start = time.time()
    old=sys.stdout
    f=open('testdata.log','w')
    sys.stdout=f
    p = subprocess.Popen(('/bin/sh','-c','curl 10.0.0.3:9918 -o /dev/null'),  stdout=sys.stdout, stderr=subprocess.STDOUT)
    
    while 1:
        end=time.time()
        #print end-start
        if end-start>30:
            p.terminate()
            break
        time.sleep(1)
        
    sys.stdout=old
    f.close()

    以上是freebsd运行环境

    下面是win7运行环境

    import os,sys
    import subprocess
    import time
    import random
    
    
    def test(t):
        sys.path.append(r'd:curl')
        start = time.time()
        timestr = time.strftime("%Y%m%d",time.gmtime(start))
        ran=random.uniform(1, 100) 
        filename="d:\curldata\"+"log_"+str(timestr)+"_"+str(ran)+".txt"
        filepath = os.path.dirname(filename)
        if not os.path.exists(filepath):
            os.mkdir(filepath)    
        f=open(filename,'w')
        old=sys.stdout
        sys.stdout=f
        #print time.strftime("
    %Y-%m-%d %H:%M:%S
    ",time.gmtime(start))
                    
                            
        #cmdstr="d:\curlcurl  10.0.0.1//data/myfun/source/resource/movies/3B0C5ACD8414D7B2918EBD75AE9843B6.mp4 -o null"
        cmdstr="d:\curlcurl --limit-rate 100B  10.0.0.1//data/myfun/source/resource/movies/583B26DE308A12046251B3F2CA7C0755.mp4 -o null"
        p = subprocess.Popen(cmdstr,stdout = sys.stdout, stderr = subprocess.STDOUT,  shell = False)
    
        while 1:
            if (p.poll()==0):
                p.terminate()
                time.sleep(1)
                break
            
        
        f.close() 
        sys.stdout=old
        print "end>>>>>>>>>>>>>>>>>>>>"
        
        
    test(100)
         

     范例3:

    import os,sys
    import subprocess
    import time
    import random
    
    
    def test():
        sys.path.append(r'd:curl')
        start = time.time()
        timestr = time.strftime("%Y%m%d%H%M%S",time.gmtime(start))
        ran=random.uniform(1, 100) 
        filename="d:\curldata\"+"log_"+str(timestr)+"_"+str(ran)+".txt"
        #filename="d:\curldata\"+"log_"+str(timestr)+".txt"
        filepath = os.path.dirname(filename)
        if not os.path.exists(filepath):
            os.mkdir(filepath)    
        f=open(filename,'w')
    
        sys.stdout=f
        cmdstr="d:\curlcurl -x 10.0.0.54:3128  http://192.168.6.199/source/BT.mp4 -o null "
    
        print cmdstr
        p = subprocess.Popen(cmdstr,stdout = f, stderr = f,  shell = False)
    
        while 1:
            if (p.poll()==0):
                p.terminate()
                time.sleep(1)
                break
    
        print "end>>>>>>>>>>>>>>>>>>>>"
        f.close() 
        
    
    test()

     范例4:

    import os,sys
    import subprocess
    import time
    import random
    import string
    
    
    def test():
        sys.path.append(r'd:curl')
        start = time.time()
        timestr = time.strftime("%Y%m%d%H%M%S",time.gmtime(start))
        ran=random.uniform(1, 100) 
        filename="d:\curldata\"+"log_"+str(timestr)+"_"+str(ran)+".txt"
        #filename="d:\curldata\"+"log_"+str(timestr)+".txt"
        filepath = os.path.dirname(filename)
        if not os.path.exists(filepath):
            os.mkdir(filepath)    
        f=open(filename,'w')
        old=sys.stdout
    
        sys.stdout=f
        #cmdstr="d:\curlcurl -x 10.0.0.54:3128  http://192.168.6.199/source/BT.mp4 -o null "
        cmdstr="d:\curlcurl -s -w %{time_total} http://www.baidu.com -o null "
        #print "--time_total --size_download --speed_download
    ",cmdstr
        p = subprocess.Popen(cmdstr,stdout = f, stderr = f,  shell = False)
        start = time.time()
        while 1:
            
            if (p.poll()==0):
                p.terminate()
                sys.stdout=old
                end=time.time()
                total_time=end-start
                #print "total time: ",total_time
                break
    
        #print "end>>>>>>>>>>>>>>>>>>>>"
        with open(filename,'r') as reader:
            data=reader.read()
            
        print data
        f_data=string.atof(data)
        i_data=int(f_data)
        if i_data==0:
            i_data+=1
    
        s_time=10-i_data
        if(s_time>0):
            print "sleep time:",s_time
            time.sleep(s_time)
            
    
    test()
  • 相关阅读:
    BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊 ——Link-Cut Tree
    BZOJ 2049 [Sdoi2008]Cave 洞穴勘测 ——Link-Cut Tree
    hdu
    hdu
    hdu
    hdu
    hdu
    hdu
    hdu
    hdu
  • 原文地址:https://www.cnblogs.com/saryli/p/5025875.html
Copyright © 2020-2023  润新知