lua清除cdn程序

来源:互联网 发布:7u分享网络赚一元 编辑:程序博客网 时间:2024/06/05 19:33

#!/bin/luarequire "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.1Accept-Encoding: identityAccept: application/jsonHost: api.edgecast.comConnection: closeAuthorization: TOK:0585c133-dfd5-4be0-erettyty232rtyuasadUser-Agent: Python-urllib/2.7]]local POST_CONTENT= [[PUT /v2/mcc/customers/3A0B/edge/purge HTTP/1.1Accept-Encoding: identityContent-Length: $$$Connection: closeAccept: application/jsonUser-Agent: Python-urllib/2.7Host: api.edgecast.comContent-Type: application/jsonAuthorization: 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, statusendfunction 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()endfunction info()    local co = coroutine.create(function ()        request(GET_CONTENT)    end)    table.insert(threads, co)endfunction 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 endendfunction 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    endendif #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') endend

0 0