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:\\curl\data\\"+"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("\n%Y-%m-%d %H:%M:%S\n",time.gmtime(start))
                
                        
    #cmdstr="d:\\curl\curl  10.0.0.1//data/myfun/source/resource/movies/3B0C5ACD8414D7B2918EBD75AE9843B6.mp4 -o null"
    cmdstr="d:\\curl\curl --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:\\curl\data\\"+"log_"+str(timestr)+"_"+str(ran)+".txt"
    #filename="d:\\curl\data\\"+"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:\\curl\curl -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:\\curl\data\\"+"log_"+str(timestr)+"_"+str(ran)+".txt"
    #filename="d:\\curl\data\\"+"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:\\curl\curl -x 10.0.0.54:3128  http://192.168.6.199/source/BT.mp4 -o null "
    cmdstr="d:\\curl\curl -s -w %{time_total} http://www.baidu.com -o null "
    #print "--time_total --size_download --speed_download\n",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()

 

posted on 2015-12-07 13:45  清明-心若淡定  阅读(1088)  评论(0编辑  收藏  举报