多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
``` -- print() 和_out() 都是打印函数,_out()可以打印对象,优先使用out print(package.path, _DIR) --local r = require('oshine.bitmap@1.2?:conf') --print(r) -- print(require('oshine.bitmap@1.2?:bitmap.conf')) -- print(require('oshine.bitmap@:bitmap.conf')) print("版本", _ver()) print("是否cli模式运行", _is_cli()) print('文本测试替换3次:', _str_replace('1.2.3.4.5.6', '.', '/', 3)) print('文本测试替换所有:', _str_replace('1.2.3.4.5.6', '.', '/')) print('文本测试截取:', _str_sub('123456', 0)) -- 123456 print('文本测试截取:', _str_sub('123456', 0, 3)) -- 123 print('文本测试截取:', _sub_ex('n', '1/2/3/4/5/6', '/')) -- 1 print('文本测试截取:', _sub_ex('ne', '1/2/3/4/5/6', '/')) -- 1/2/3/4/5 print('文本测试截取:', _sub_ex('r', '1/2/3/4/5/6', '/')) -- 6 print('文本测试截取:', _sub_ex('re', '1/2/3/4/5/6', '/')) -- 2/3/4/5/6 print('文本测试截取:', _sub_ex('n', '123456789', 3)) -- 123 print('文本测试截取:', _sub_ex('ne', '123456789', 3)) -- 123456 print('文本测试截取:', _sub_ex('r', '123456789', 3)) -- 789 print('文本测试截取:', _sub_ex('re', '123456789', 3)) -- 456789 print('try cache 测试(_try函数和普通的try catch 不同,具体用法请看下方例子)') local sql_db = woo.orm:new() _try(function() print1(sql_db:open()) end, function(err) print("got err in try body:", err) end, function() print("_try finally end") sql_db:close() end) print(woo.gd.DRAW_OVER, woo.gd.RESIZE_LANCZOS2, woo.gd.SURFACE_REPEAT_NONE) -- debug test print "Debug:" local info = debug.getinfo(1, "Slunf") _out(info, '\n') print('写到文件测试开始:') _file_save('require_test.woo', 'return {1,2,3}') print('读取文件测试开始:') print('读取内容:', _cat('require_test.woo', true)) print('模块引用测试开始:') _out(require("require_test"), '\n') _sleep(100) print("延迟100毫秒后执行继续。。。") print('取启动参数(如果执行:woo ab.woo params1 params2 ...) 将会获得{params1 params2 ...}') _out('取程序启动参数:', _args(), '\n') print("全局数据,主程序退出后消失,全部线程共享的数据集合,使用前请小心") _out('全局数据存取:', _data_get("key1", _data_put("key1", "提交的数据...")), '\n') print('取时间测试:') -- 2020-08-19 13:01:51 print(_date("Y-m-d H:i:s")) print(_date("D M j G:i:s T Y")) print(_date("F j, Y, g:i a")) print(_date("h-i-s, j-m-y, w Day z ")) print("base64编码:" .. _base64_decode(_base64_encode(_time()))) print("当前时间戳:" .. _time()) local r = { kl = 123 } _out('输出数组:', r, '\n') _out('输出json编码:', _json_encode(r), '\n') print("crc32:" .. _crc32(_time())) print("md5:" .. _md5(_time())) print("sha1:" .. _sha1(_time())) local url = _url_encode('http://www.baidu.com?u=1&p=+90') print("url编码:" .. url) print("url解码:" .. _url_decode(url)) print('===函数传参测试:===') local fna = function(p, ...) return { p, ... } end local function fnb(p) return fna(p, 2, 3, 4, 5) end _out("函数传参结果:", fnb(1), '\n') print('===openssl加密解密:===') local cipher = woo.cipher:new() -- 私钥生成 -- openssl genrsa -out rsa_private_key.pem 1024 local privateKey = [[ -----BEGIN RSA PRIVATE KEY----- MIICWwIBAAKBgQDcGsUIIAINHfRTdMmgGwLrjzfMNSrtgIf4EGsNaYwmC1GjF/bM h0Mcm10oLhNrKNYCTTQVGGIxuc5heKd1gOzb7bdTnCDPPZ7oV7p1B9Pud+6zPaco qDz2M24vHFWYY2FbIIJh8fHhKcfXNXOLovdVBE7Zy682X1+R1lRK8D+vmQIDAQAB AoGAeWAZvz1HZExca5k/hpbeqV+0+VtobMgwMs96+U53BpO/VRzl8Cu3CpNyb7HY 64L9YQ+J5QgpPhqkgIO0dMu/0RIXsmhvr2gcxmKObcqT3JQ6S4rjHTln49I2sYTz 7JEH4TcplKjSjHyq5MhHfA+CV2/AB2BO6G8limu7SheXuvECQQDwOpZrZDeTOOBk z1vercawd+J9ll/FZYttnrWYTI1sSF1sNfZ7dUXPyYPQFZ0LQ1bhZGmWBZ6a6wd9 R+PKlmJvAkEA6o32c/WEXxW2zeh18sOO4wqUiBYq3L3hFObhcsUAY8jfykQefW8q yPuuL02jLIajFWd0itjvIrzWnVmoUuXydwJAXGLrvllIVkIlah+lATprkypH3Gyc YFnxCTNkOzIVoXMjGp6WMFylgIfLPZdSUiaPnxby1FNM7987fh7Lp/m12QJAK9iL 2JNtwkSR3p305oOuAz0oFORn8MnB+KFMRaMT9pNHWk0vke0lB1sc7ZTKyvkEJW0o eQgic9DvIYzwDUcU8wJAIkKROzuzLi9AvLnLUrSdI6998lmeYO9x7pwZPukz3era zncjRK3pbVkv0KrKfczuJiRlZ7dUzVO0b6QJr8TRAA== -----END RSA PRIVATE KEY----- ]] -- 公钥: 根据私钥生成 -- openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem local publicKey = [[ -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcGsUIIAINHfRTdMmgGwLrjzfM NSrtgIf4EGsNaYwmC1GjF/bMh0Mcm10oLhNrKNYCTTQVGGIxuc5heKd1gOzb7bdT nCDPPZ7oV7p1B9Pud+6zPacoqDz2M24vHFWYY2FbIIJh8fHhKcfXNXOLovdVBE7Z y682X1+R1lRK8D+vmQIDAQAB -----END PUBLIC KEY----- ]] local isToBase64 = true local rsaEnStr = cipher:rsaEncode("hello world!", publicKey, isToBase64) print('openssl加密结果:', rsaEnStr) local isFromBase64 = true local isToString = true local rsaDeStr = cipher:rsaDecode(rsaEnStr, privateKey, isFromBase64, isToString) print('openssl解密结果:', rsaDeStr) print('===aes加密解密:===') local encrypt_string = "hello world!" local encrypt_psw = "这是密码" local iv = nil -- 这个向量可忽略掉 local enStr = cipher:aesEncode(encrypt_string, encrypt_psw, iv) print("aes加密结果", enStr) local deStr = cipher:aesDecode(enStr, encrypt_psw, iv) print("aes解密结果", deStr) print('内部模块http测试,内部模块') -- 引用内部模块:http local http = woo.http:new() --http:proxy({ channel = 'socks5', host = '127.0.0.1', port = '1288', username = '', password = '' }) local response, error_message = http:get("http://www.baidu.com", { query = "page=1", timeout = "3s", headers = { Accept = "*/*", --['user-agent'] = 'chrome ...' }, }) print('错误信息:', error_message) local rule_re = '<title>(.*?)</title>' _out('访问百度首页并使用正则[' .. rule_re .. ']匹配标题:', _re_match(response.body, rule_re)[1][2]) -- 下方注释打开即可测试 --print("读取控制台输入:", _readline("请输入字符:")) --print("读取控制台密码输入:", _readline("请输入字符:", true)) print('当前系统', _os()) -- windows darwin linux print('系统架构arch', _os(true)) -- amd64 arm64 x86_64 armvhl ... print('系统缓存目录', _temp()) --/tmp print('系统家目录', _home()) -- ~/home print('当前woo执行目录', _pwd()) print('uuid获取', _uuid()) print('多项选择,真假测试', _choose(1 > 2, '123', '321')) print('多项选择,数字定位', _choose(2, '123', '321')) local a = 12 print('位或', math.bor(a, 3)) print('位与', math.band(a, 3)) print('位取反', math.bnot(a, 3)) print('位异或', math.bxor(a, 3)) print('左移', math.lshift(a, 3)) print('右移', math.rshift(a, 3)) local jz = _tenToAny(5154212501, 64) print('10进制到任意进制', jz) -- 请勿超过72进制 print('任意进制到10进制', _anyToTen(jz, 64)) print('foreach数组迭代') _foreach({ 1, 2, 3, 4, 5 }, function(k, v) -- 正常迭代数组 print(k, v) if k == 4 then return true end end) _foreach("123456789 abcd", function(k, v) -- 迭代字符串 print(k, v, _ascii(v)) end) _foreach(2, function(k) -- 循环2次 _foreach("abcd", function(k1, v) print(k, k1, v) end) end) _foreach(true, function() -- 循环无数次,返回true可终止循环 print('loop循环...') return true end) _out('table/数组合并', _merge({ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, 10, 11, 12, "13"), '\n') -- 数组合并也可以merge字符串数值类型 print('多线程执行脚本:') local run_cod = [[ _foreach(10, function(p) print('sub thread:',_ARGS[1]) _chan_put(false, { "来自", _ARGS[1], p }) -- 使用_ARGS来获取主线程传递的参数 end) print('sub thread end', _ARGS[1]) ]] _file_put_contents('run_test.woo', run_cod) _run('run_test.woo', "线程1") -- 传递参数:线程1 _run('run_test.woo', "线程2") -- 传递参数:线程2 _foreach(20, function(p) _out('主线程loop') local res = _chan_get(true) _out('主线程loop,取线程提交值', res, "\n") end) print('打印所有可见字符的ascii码测试开始:') _foreach(93, function(p) print("ascii码测试-ascii(" .. (p + 32) .. ")=" .. _chr(p + 32)) -- 这里打印所有可见字符以及其对应的ascii码 end) print('字符串到字节集测试:') -- 字节集形似{36,56,54,98,102,195,10},单个字节介于0-255之间 local r1 = _bytes('你好:123') _out(r1, '\n') print('字节集到字符串测试:', _strs(r1)) print('sockets模拟http请求测试(正常情况下,请勿这样使用,请使用正规的http模块,这里仅仅探索sockets使用方法):') local sock = woo.sockets:new() local ok = sock:dial('tcp', 'www.baidu.com:80', function() print('在这个函数内,sockets有效,函数结束后,sockets自动关闭') _out('拨通远程ip ok', '\n') r = sock:write(_bytes("GET / HTTP/1.1\r\n" .. "Host: www.baidu.com\r\n" .. "User-Agent: curl/7.10\r\n" .. "Connection: Close\r\n\r\n")) _out('发送http头部信息字节长度:', r, '\n') --r = sock:write(0x05, 0x05) print('===字节集在转换为字符串时需要注意,单字节大小不能超过255,应当在0-255范围内,请勿修改字节集中的数据===') local b, e = sock:readAll(false) --local b, e = sock:readAll(true) rule_re = '<title>(.*?)</title>' _out('sockets模拟http请求 访问百度首页并使用正则[' .. rule_re .. ']匹配标题:', _re_match(_strs(b), rule_re)[1][2], '\n') --print("sockets模拟http请求最终结果:", b, e) end) print("websockets测试(onMessage每次读取的字节大小为最大10KB,超过10KB请使用变量来统计):", 0xa + 1e6) --local wsock = require(':ws'):new() local wsock = woo.ws:new() local index = 0 wsock:with(function(len, bytes) _out('得到ws服务器返回字节集长度:', len, '\n') _out('得到ws服务器返回字节集转换为字符串:', _strs(bytes), '\n') wsock:write("再给你条消息" .. index) index = index + 1 if index > 2 then return true -- 这里返回true,即可停止读取 end end, function() _out('拨通远程ws-ip链接状态:', r, '\n') r = wsock:write("你好服务器") _out('ws写入长度:', r, '\n') end, function() if not wsock:reConnect() then print('reConnect fail,exit') return true end end, function(err) _out('得到错误信息:', err) end) :dial("ws://123.207.136.134:9010/ajaxchattest", "http://123.207.136.134:9010/") print('文件流读取测试:') local lfs = woo.lfs local mode = 'A|RW|C' print('===文件model描述-> A:append(追加),RW:readWrite(读写),RO:readOnly(只读),WO:writeOnly(只写),C:create(不存在则创建文件)===') print('创建测试文件test.txt') e = lfs.with('test.txt', function(f) -- lfs不是类,直接用.即可调用内部方法 print('开始写入测试文本') f.write("hello world!\r\nline2\nline3") print('写入测试文本完毕,文件读写指针在文件末,需要用seek移动到首,才能继续读文件') f.seek(0, "S") _foreach(true, function(p) local bts, err = f.readLine() if err then -- 读取到文件尾部则返回true 跳出循环即可 --print(err) return true end local s = _strs(bts) print(s) end) end, mode) print('错误信息:', e) print('进度下载测试:') _wget('deepin.jpg', 'https://storage.deepin.org/forum/2020123007243212_1609327472.jpg', { cookies = { BIDUPSID = '16626'; PSTM = '1609138339' }, headers = { ['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36' } } or nil, function(current, total) print('已下载:', current, '总共:', total) end) print('简便下载文件方式,也可直接下载网页内容到文件') _wget('r.html', 'http://www.baidu.com') --退出当前主线程 os.exit(1) ```