protocol demo about cache

This commit is contained in:
JamesonHuang 2015-06-17 22:58:49 +08:00
parent 9ed4cc7551
commit e9f833d7f3
12 changed files with 15054 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,30 @@
#!/usr/bin/env python
# coding=utf-8
import urllib
import urllib2
class CacheUtils:
def cbk(a, b, c):
'''''回调函数
@a: 已经下载的数据块
@b: 数据块的大小
@c: 远程文件的大小
'''
per = 100.0 * a * b / c
if per > 100:
per = 100
print '%.2f%%' % per
def download(self, url, local):
urllib.urlretrieve(url, local, cbk)
def cache(self, url):
fileName = url.split('/')[-1]
req = urllib2.Request(url)
#req.add_header('Range', 'bytes=0-7000')
response = urllib2.urlopen(req)
buffer = response.read()
with open("./cache/img.png", "a+") as fp:
fp.write(buffer)

View File

@ -0,0 +1,10 @@
test1test2test3test4test5test6test7test8test9test1
test2
test3
test4
test5
test6
test7
test8
test9
test1test2test3test4test5test6test7test8test9

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

View File

@ -0,0 +1,24 @@
#!/usr/bin/env python
# coding=utf-8
"""
#fileIO read & print
with open("py.txt") as fp:
#data = fp.read()
data = fp.readline()
print(data)
"""
"""
#fileIO foreach write
with open("py.txt", 'a+') as fp:
for num in range(1,10):
#fp.write("test" + str(num) + "\n" )
fp.writelines("test" + str(num))
"""
with open("py.txt", 'a+') as fp:
for line in fp.readlines():
line = line.strip() #去除首尾空白
print(line)

View File

@ -0,0 +1,75 @@
#!/usr/bin/env python
# coding=utf-8
import urllib
import urllib2
import os
def cbk(a, b, c):
'''''回调函数
@a: 已经下载的数据块
@b: 数据块的大小
@c: 远程文件的大小
'''
per = 100.0 * a * b / c
if per > 100:
per = 100
print '%.2f%%' % per
""" urllib下载
url = 'http://www.sina.com.cn'
#local = '/home/rh/my_code/OpenWrt_Luci_Lua/1_7.http_proxy_server/python/testCode/sina.html'
#local = '/home/rh/my_code/OpenWrt_Luci_Lua/1_7.http_proxy_server/python/testCode/test.html'
#local = './cache/backup'
fileName = url.split('/')[-1]
local = os.path.join('./cache',fileName)
urllib.urlretrieve(url, local, cbk)
"""
#url = "http://download.meizu.com/Firmware/Flyme/m1_note/4.2/cmcc/20150507154711/61746475/update.zip"
url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superplus/img/logo_white_ee663702.png"
req = urllib2.Request(url)
req.add_header('Range', 'bytes=0-7000')
response = urllib2.urlopen(req)
buffer = response.read()
with open("./cache/test1.png", "a+") as fp:
fp.write(buffer)
req = urllib2.Request(url)
req.add_header('Range', 'bytes=7001-14175')
response = urllib2.urlopen(req)
buffer = response.read()
with open("./cache/test2.png", "a+") as fp:
fp.write(buffer)
""" urllib2下载 request失败
url = 'http://www.sina.com.cn'
req = urllib2.Request(url)
#req.add_header('range','byte:1-100')
response = urllib2.urlopen(req)
file = response.read()
with open("urllib2test.html", "a+") as fp:
fp.write(file)
"""
"""urlopener 失败"""
url = 'http://www.sina.com.cn'
opener = urllib2.build_opener()
#不加头信息则出现403错误和乱码
opener.addheaders = [('User-agent', 'Mozilla/5.0')];
#opener.addheaders = [('range', 'byte:1-100')]
htmlAll = opener.open( url ).read()
reg1Floor = '<div class="msgfont">(.*?)</div>'
#文件保存编码和文件编辑编码都是utf-8所以decode一次不然会出现乱码但是不影响结果。
#htmlAll.decode('utf-8')
print(htmlAll)

View File

@ -0,0 +1,16 @@
#!/usr/bin/env python
# coding=utf-8
import urllib2
#url = "http://download.meizu.com/Firmware/Flyme/m1_note/4.2/cmcc/20150507154711/61746475/update.zip"
url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superplus/img/logo_white_ee663702.png"
#url = "http://www.sina.com.cn"
req = urllib2.Request(url)
req.add_header('Range', 'bytes=7001-14000')
response = urllib2.urlopen(req)
#print(response.info())
html = response.read()
with open("test4.png", "a+") as fp:
fp.write(html)
#print html