lua清除cdn程序
2014-04-06 19:45
218 查看
#!/bin/lua
require "socket"
local ID= '330B'
local TOK= '0585c133-dfd5-4be0-816a-erettyty232rtyuasad'
local URL_LIST_FILE= './cdn.txt'
local GET_CONTENT= [[GET /v2/reporting/customers/3A0B/media/8/cachestats?begindate=2010-01-01&enddate=2013-12-01 HTTP/1.1
Accept-Encoding: identity
Accept: application/json
Host: api.edgecast.com
Connection: close
Authorization: TOK:0585c133-dfd5-4be0-erettyty232rtyuasad
User-Agent: Python-urllib/2.7
]]
local POST_CONTENT= [[PUT /v2/mcc/customers/3A0B/edge/purge HTTP/1.1
Accept-Encoding: identity
Content-Length: $$$
Connection: close
Accept: application/json
User-Agent: Python-urllib/2.7
Host: api.edgecast.com
Content-Type: application/json
Authorization: TOK:0585c133-dfd5-4be0-816a-a19db2319ea9
{"MediaPath": "%s","MediaType": 8}]]
local host= 'api.edgecast.com'
local threads = {}
function receive(connection)
connection:settimeout(0)
local s, status, partial = connection:receive('*a')
if status == "timeout" then
coroutine.yield(connection)
end
return s or partial, status
end
function request(data)
local c = assert(socket.connect(host, 80))
local count = 0
--print(data)
assert(c:send(data))
while true do
local s, status, partial = receive(c)
count = count + #(s or partial)
if status == "closed" then
break
end
if not s then
print(status, partial)
else
print(s)
end
end
c:close()
end
function info()
local co = coroutine.create(function ()
request(GET_CONTENT)
end)
table.insert(threads, co)
end
function clear()
local cdn_file = io.open(URL_LIST_FILE,"r")
local content
local count
local continue
for line in cdn_file:lines() do
line= line:gsub("^%s+", ""):gsub("%s+$", "")
if(not string.len(line)) then
continue= false
else
continue= true
end
if continue== true then
content= string.format(POST_CONTENT, line)
_, pos= string.find(content, "\n\n")
count= string.len(string.sub(content, pos+1, -1))
--print(string.sub(content, pos+1, -1))
content= content:gsub("($$$)", count)
print(content)
local co = coroutine.create(function ()
request(content)
end)
table.insert(threads, co)
end
end
end
function dispatch()
local i = 1
local connections = {}
while true do
if threads[i] == nil then
if threads[1] == nil then
break
end
i = 1
connections = {}
end
local status, res = coroutine.resume(threads[i])
if not res then
table.remove(threads, i)
else
i = i + 1
connections[#connections + 1] = res
if #connections == #threads then
socket.select(connections)
end
end
end
end
if #arg <1 then
print('parameter error.')
else
if arg[1]== 'info' then
info()
dispatch()
elseif arg[1]== 'clear' then
clear()
dispatch()
else
print('Usage: lua cdn.lua info, lua cdnlua clear')
end
end
#!/bin/lua
require "socket"
local ID= '330B'
local TOK= '0585c133-dfd5-4be0-816a-erettyty232rtyuasad'
local URL_LIST_FILE= './cdn.txt'
local GET_CONTENT= [[GET /v2/reporting/customers/3A0B/media/8/cachestats?begindate=2010-01-01&enddate=2013-12-01 HTTP/1.1
Accept-Encoding: identity
Accept: application/json
Host: api.edgecast.com
Connection: close
Authorization: TOK:0585c133-dfd5-4be0-erettyty232rtyuasad
User-Agent: Python-urllib/2.7
]]
local POST_CONTENT= [[PUT /v2/mcc/customers/3A0B/edge/purge HTTP/1.1
Accept-Encoding: identity
Content-Length: $$$
Connection: close
Accept: application/json
User-Agent: Python-urllib/2.7
Host: api.edgecast.com
Content-Type: application/json
Authorization: TOK:0585c133-dfd5-4be0-816a-a19db2319ea9
{"MediaPath": "%s","MediaType": 8}]]
local host= 'api.edgecast.com'
local threads = {}
function receive(connection)
connection:settimeout(0)
local s, status, partial = connection:receive('*a')
if status == "timeout" then
coroutine.yield(connection)
end
return s or partial, status
end
function request(data)
local c = assert(socket.connect(host, 80))
local count = 0
--print(data)
assert(c:send(data))
while true do
local s, status, partial = receive(c)
count = count + #(s or partial)
if status == "closed" then
break
end
if not s then
print(status, partial)
else
print(s)
end
end
c:close()
end
function info()
local co = coroutine.create(function ()
request(GET_CONTENT)
end)
table.insert(threads, co)
end
function clear()
local cdn_file = io.open(URL_LIST_FILE,"r")
local content
local count
local continue
for line in cdn_file:lines() do
line= line:gsub("^%s+", ""):gsub("%s+$", "")
if(not string.len(line)) then
continue= false
else
continue= true
end
if continue== true then
content= string.format(POST_CONTENT, line)
_, pos= string.find(content, "\n\n")
count= string.len(string.sub(content, pos+1, -1))
--print(string.sub(content, pos+1, -1))
content= content:gsub("($$$)", count)
print(content)
local co = coroutine.create(function ()
request(content)
end)
table.insert(threads, co)
end
end
end
function dispatch()
local i = 1
local connections = {}
while true do
if threads[i] == nil then
if threads[1] == nil then
break
end
i = 1
connections = {}
end
local status, res = coroutine.resume(threads[i])
if not res then
table.remove(threads, i)
else
i = i + 1
connections[#connections + 1] = res
if #connections == #threads then
socket.select(connections)
end
end
end
end
if #arg <1 then
print('parameter error.')
else
if arg[1]== 'info' then
info()
dispatch()
elseif arg[1]== 'clear' then
clear()
dispatch()
else
print('Usage: lua cdn.lua info, lua cdnlua clear')
end
end
相关文章推荐
- lua的coroutine 协同程序实现迭代器
- php文件夹删除、php清除缓存程序
- 第一个Lua程序
- 小程序清除interval
- 病毒清除了,所有的EXE程序还是不能启动
- Lua中的协同程序 coroutine
- Android WebView 浏览器设置默认程序&清除默认设置
- 第一个LUA程序
- 用Lua编写iOS程序
- lua之协同程序coroutine
- 编译 cocos2d-x 程序:eclipse (lua)
- 编写DEBUG程序清除CMOS密码
- 将Lua嵌入IOS程序
- 高手详解木马程序和病毒的原理和清除的通用解法
- vs2008程序退出时自动清除注册表!
- 在Lua程序中使用MySQL的教程
- Lua协同程序
- Lua基础 小结(两个Lua程序示例)
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
- lua中的协同程序