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. Circuit translates I2C voltages

    This Design Idea explores level-shifting an I2C bus from 5V/ground (positive domain) to ground/–5V ( ...

  2. How to implement *All-Digital* analog-to-digital converters in FPGAs and ASICs

    When we engineers look at the complexity of system design these days, we are challenged with crammin ...

  3. Codeforces Round #FF (Div. 1) B. DZY Loves Modification

    枚举行取了多少次,如行取了i次,列就取了k-i次,假设行列单独贪心考虑然后相加,那么有i*(k-i)个交点是多出来的:dpr[i]+dpc[k-i]-i*(k-i)*p 枚举i取最大值.... B. ...

  4. g++ 链接*.a静态库 方法

    g++在链接*.a静态库时,直接作为object使用,不需要使用-l ,但是需要注意调整顺序,被依赖的文件放在后面. 如: g++  -g  -o0 -Wno-deprecated  -I$(INCL ...

  5. 判断openfire用户的状态

    /** * 判断openfire用户的状态 * 说明 :必须要 openfire加载 presence 插件,同时设置任何人都可以访问 * /status?jid=user1@my.openfire. ...

  6. 如何使用花生壳 发布WCF服务 进行外网访问 z

    http://www.cnblogs.com/wanglg/p/5375230.html 当我们发布WCF服务的时候,可以直接通过服务器的域名或者IP进行. 但是如果仅仅是通过花生壳进行域名解析,需要 ...

  7. jsp button提交表单

    表单提交可以用submit 也可以用button,下面介绍下面三种方式: 方法1 <form action = "提交的地址"> <input type=&quo ...

  8. Python mongoHelper模块

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' Defines a MongoOperator class and allows you to m ...

  9. (1)风色从零单排《C++ Primer》 一个简单的c++程序

    从零单排<C++ Primer> --(1)一个简单的c++程序 本次学习收获 0.写在前面 风色以前上过C++的课程,然而当时并没有认真去学,基本不能使用c++来作项目开发. 这次又一次 ...

  10. 极客Web开发资源大荟萃

    前端开发已经成为当前炙手可热的技术之一.此次我们总结的前端开发包含了相关技术和流行趋势,希望从中大家可以挖掘你们所需要的,并带给你们最有价值的帮助!原文来自:极客标签 使用代码回放来愉快地学习前端知识 ...