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 :
end=time.time()
#print end-start
if end-start>:
p.terminate()
break
time.sleep() 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(, )
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 :
if (p.poll()==):
p.terminate()
time.sleep()
break f.close()
sys.stdout=old
print "end>>>>>>>>>>>>>>>>>>>>" test()

范例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(, )
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 :
if (p.poll()==):
p.terminate()
time.sleep()
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(, )
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 : if (p.poll()==):
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==:
i_data+= s_time=-i_data
if(s_time>):
print "sleep time:",s_time
time.sleep(s_time) test()

python --curl重定向到文件范例的更多相关文章

  1. Python第十天 print >> f,和fd.write()的区别 stdout的buffer 标准输入 标准输出 从控制台重定向到文件 标准错误 重定向 输出流和输入流 捕获sys.exit()调用 optparse argparse

    Python第十天   print >> f,和fd.write()的区别    stdout的buffer  标准输入 标准输出  从控制台重定向到文件  标准错误   重定向 输出流和 ...

  2. Python学习笔记015——文件file的常规操作之四(输入重定向)

    windows命令提示符(cmd.exe)和Linux Shell(bash等)均可通过">"或”>>“将输出重定向.其中, ">"表示 ...

  3. python输出重定向

    0表示标准输入1表示标准输出2表示标准错误输出> 默认为标准输出重定向,与 1> 相同2>&1 意思是把 标准错误输出 重定向到 标准输出.&>file 意思是 ...

  4. Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块

    Python第五天   文件访问    for循环访问文件    while循环访问文件   字符串的startswith函数和split函数  linecache模块 目录 Pycharm使用技巧( ...

  5. Python标准输出重定向

    目录 Python标准输出重定向 声明 一. 背景知识 二. 重定向方式 2.1 控制台重定向 2.2 print >>重定向 2.3 sys.stdout重定向 2.4 上下文管理器(C ...

  6. PHP如何通过CURL上传文件

    PHP使用CURL上传文件只需发送一个POST请求就可以了,在请求中设置某个字段为需要上传的文件全路径,并且以“@”开头,然后使用CURL把该变量以POST方式发送到服务器,在服务端即可以从超级全局变 ...

  7. python批量处理excel文件数据

    https://www.zhihu.com/question/39299070?sort=created 作者:水中柳影链接:https://www.zhihu.com/question/392990 ...

  8. [python]自动化将markdown文件转成html文件

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  9. Linux下执行的java命令重定向到文件中的方法

    在Linux下通常会执行如:java -version 的命令, 但是,命令只是打印到了屏幕上不能重定向到文件中或标准输出流中. 此时需要将错误输出流重定向到标准输出流中就可以得到了. 比如:java ...

随机推荐

  1. Practice safe dc/dc converter

    Short-circuit protection is an obvious requirement for a power supply, especially when its load conn ...

  2. spring学习之@ModelAttribute运用详解

    @ModelAttribute使用详解 1.@ModelAttribute注释方法     例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法 ...

  3. head first---------facade design pattern

    head first----------外观模式或者门面模式         外观模式又名门面模式:提供了一个统一的接口,用来访问子系统中的一群接口.外观模式定义了一个高层接口,从而让子系统更容易使用 ...

  4. (转)H264通过RTMP发布 V2.0 (Red5 Wowza 测试通过)

    直接上代码 // demo.cpp : 定义控制台应用程序的入口点.//#include "stdafx.h"#includeextern "C"{#inclu ...

  5. (转)h264中avc和flv数据的解析

    计算 AVCDecoderConfigurationRecord  得到 CodecPrivateData 数据(只有第一帧需要): 计算 NALUs 得到帧数据. 计算 AVCDecoderConf ...

  6. linux下启动tomcat出现“This file is needed to run this program ”

    使用sh startup.sh启动tomcat 出现This file is needed to run this program 原因.sh文件都不是可执行文件,于是找到命令: chmod +x * ...

  7. AE 打开工具箱工具的对话框

    The best approach to run a system tool—or any tool or model created, and saved in a toolbox from a b ...

  8. pm2 相关命令

    pm2 相关命令 转载 2017年02月18日 22:59:48   二.安装 Linux Binaries下载地址:https://nodejs.org/dist cd oneinstack/src ...

  9. iOS 改动toolbar里面文字的字体和大小

    使用NSDictionaty来设置文本的属性: NSDictionary * attributes = @{NSFontAttributeName: [UIFont fontWithName:@&qu ...

  10. 获取音视频文件AVMetadata数据

    获取音视频文件AVMetadata数据 问题来源: http://stackoverflow.com/questions/16318821/extracting-mp3-album-artwork-i ...