python 简单碎片读取 随笔

工具类 testwrite.py

#coding:utf-8

import os
import random
l1=['1','2','3','4','5','6']
def wtof(path,txtstr):
if not os.path.exists(path):
f=open(path,'w')
f.close()
f=open(path,'a')
f.write(txtstr)
f.close()
l2=['6','2','1','3','4','5','0']

def piece(start,buf,path):

#插入碎片注意

f=open(path,'r+')
f.seek(start)
f.write(buf)
f.close()
def delpiece(path):
if os.path.exists(path):
os.remove(path)
def cr_piece(path):
if os.path.exists(path):
os.remove(path)
f=open(path,'w')
f.close()
for i in l2:

piece(int(i),str(i),'ss.text')

实现 默认分十个片段

#coding:utf-8
import urllib2
import random
import testwrite as tw
handler = urllib2.HTTPHandler()
request = urllib2.Request(url='http://www.jeapedu.com')
#request.headers['Range'] = 'bytes=%s-%s' % (0, 200)
f = urllib2.urlopen(request);
print f.headers['content-length']
total=f.headers['content-length']
print total

#计算每个片段字节数
pageSize=int((int(total))/9)
if int((int(total))%10)==0:
pageSize=int((int(total))/10)
print pageSize
sum=0
l1=[0,1,2,3,4,5,6,7,8,9]
tw.delpiece('test.html')
tw.cr_piece('test.html')
while len(l1)>0:
index=random.choice(list(l1))
start=index*pageSize
end=start+pageSize-1
request.headers['Range'] = 'bytes=%s-%s' % (start, end)
if index==9:
request.headers['Range'] = 'bytes=%s-' % (start)
ff=urllib2.urlopen(request)
print ff
buf=ff.read()
tw.piece(start,buf,"test.html")
l1.remove(index)

运行:
python piece.py 基本好使


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值