您的位置:首页 > 编程语言 > Lua

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: