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. IDA 中文字符串

    http://www.pediy.com/kssd/pediy05/pediy50528.htm Ida Pro 的默认设置里对中文字串的支持比较差,对于首字节大于'E0'的都显示成?了.其实...  ...

  2. HelloWorld 和相关设置

    写这篇文章的初衷很简单,就是想再一次证明 IntelliJ IDEA 对于 Java 开发人员来说,确实比 eclipse 要好用得多,鉴于目前市面上关于 IntelliJ IDEA 的教程比较少,叙 ...

  3. Mysql字符串连接函数 CONCAT()与 CONCAT_WS()

    从数据库里取N个字段,然后组合到一起用“,”分割显示,起初想到用CONCAT()来处理,好是麻烦,没想到在手册里居然有提到 CONCAT_WS(),非常好用. CONCAT_WS(separator, ...

  4. ionic开发环境搭建之ios

    前言 公司在做完ionic androud版后就开始做ios版,虽然ios的坑我觉得比起androud少了很多,但是作为第一次接触ios的我来说,环境实在太麻烦,从搭环境到打包一个正式版的ios ap ...

  5. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  6. stl map中的lower_bound和 upper_bound

    map中的lower_bound和upper_bound的意思其实很简单,就两句话: map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针 map::upper_ ...

  7. java垃圾回收机制--可达性算法

    先说一些题外话,Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区,这些区分为线程私有区和线程共享区 1.线程私有区 a.程序计数器 记录正在执行的虚拟机字节码指令地址 ...

  8. apache环境 php开启intl扩展

    将php目录下的icu开头的所有dll文件copy到apache/bin目录 再开启 extension=php_intl.dll 扩展,重启apache.

  9. IIs 中运行asp程序出现“An error occurred on the server when processing the URL. Please contact the system administrator.”错误

    在个人的win08系统上使用IIs运行 asp程序结果出现了以下错误 An error occurred on the server when processing the URL. Please c ...

  10. 屏幕实时显示键盘鼠标操作软件keycastow,适合做视频教程

    屏幕实时显示键盘鼠标操作软件keycastow,适合做视频教程 学习了:https://www.52pojie.cn/thread-535154-1-1.html 进行键盘按键的屏幕实时显示: