mirror of
https://github.com/JamesonHuang/OpenWrt_Luci_Lua.git
synced 2024-11-23 22:00:11 +00:00
protocol demo about cache
This commit is contained in:
parent
9ed4cc7551
commit
e9f833d7f3
BIN
1_7.http_proxy_server/curl-test/part1
Normal file
BIN
1_7.http_proxy_server/curl-test/part1
Normal file
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
7438
1_7.http_proxy_server/python/testCode/cache/backup
vendored
Normal file
7438
1_7.http_proxy_server/python/testCode/cache/backup
vendored
Normal file
File diff suppressed because one or more lines are too long
7438
1_7.http_proxy_server/python/testCode/cache/www.sina.com.cn
vendored
Normal file
7438
1_7.http_proxy_server/python/testCode/cache/www.sina.com.cn
vendored
Normal file
File diff suppressed because one or more lines are too long
30
1_7.http_proxy_server/python/testCode/cacheUtil.py
Normal file
30
1_7.http_proxy_server/python/testCode/cacheUtil.py
Normal 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)
|
||||
|
||||
|
10
1_7.http_proxy_server/python/testCode/py.txt
Normal file
10
1_7.http_proxy_server/python/testCode/py.txt
Normal file
@ -0,0 +1,10 @@
|
||||
test1test2test3test4test5test6test7test8test9test1
|
||||
test2
|
||||
test3
|
||||
test4
|
||||
test5
|
||||
test6
|
||||
test7
|
||||
test8
|
||||
test9
|
||||
test1test2test3test4test5test6test7test8test9
|
BIN
1_7.http_proxy_server/python/testCode/res.png
Normal file
BIN
1_7.http_proxy_server/python/testCode/res.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
23
1_7.http_proxy_server/python/testCode/test.html
Normal file
23
1_7.http_proxy_server/python/testCode/test.html
Normal file
File diff suppressed because one or more lines are too long
BIN
1_7.http_proxy_server/python/testCode/test1.png
Normal file
BIN
1_7.http_proxy_server/python/testCode/test1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.8 KiB |
BIN
1_7.http_proxy_server/python/testCode/test2.png
Normal file
BIN
1_7.http_proxy_server/python/testCode/test2.png
Normal file
Binary file not shown.
24
1_7.http_proxy_server/python/testCode/testFileIO.py
Normal file
24
1_7.http_proxy_server/python/testCode/testFileIO.py
Normal 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)
|
75
1_7.http_proxy_server/python/testCode/testUrllib.py
Normal file
75
1_7.http_proxy_server/python/testCode/testUrllib.py
Normal 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)
|
||||
|
||||
|
16
1_7.http_proxy_server/python/testCode/url2.py
Normal file
16
1_7.http_proxy_server/python/testCode/url2.py
Normal 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
|
Loading…
Reference in New Issue
Block a user