博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python --curl重定向到文件范例
阅读量:4354 次
发布时间:2019-06-07

本文共 3769 字,大约阅读时间需要 12 分钟。

 
import sysimport osimport subprocessimport timestart = time.time()old=sys.stdoutf=open('testdata.log','w')sys.stdout=fp = 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=oldf.close()

以上是freebsd运行环境

下面是win7运行环境

import os,sysimport subprocessimport timeimport randomdef 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,sysimport subprocessimport timeimport randomdef 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,sysimport subprocessimport timeimport randomimport stringdef 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()

 

转载于:https://www.cnblogs.com/saryli/p/5025875.html

你可能感兴趣的文章
codevs 2803 爱丽丝·玛格特罗依德
查看>>
java8的十大新特性
查看>>
Ms sql server 数据类型说明
查看>>
shadow密码文件
查看>>
归并排序及优化(Java实现)
查看>>
kubernates使用kubeadm安装
查看>>
图说超线程技术(Hyper-Threading Technology)
查看>>
首页跳转
查看>>
insert into select 与select into from -- sql 批量插入
查看>>
剑指offer-二叉树的镜像
查看>>
关于 Activity 中 startActivityForResult 和 onActivityResult
查看>>
Entity Framework Code First (一)Conventions
查看>>
asp.net程序集强签名
查看>>
numpy数据集
查看>>
为什么Python是最适合初创公司的编程语言?
查看>>
Python中scatter函数参数用法详解
查看>>
iOS开发之支付宝一篇通
查看>>
快速排序,归并排序
查看>>
Windows下Subversion的安装及配置
查看>>
【cs229-Lecture4】GLMS:选定指数分布族,如何用它来推导出GLM?
查看>>