[lua]紫猫lua教程-命令宝典-L1-01-12. 临时补充2

1.lua的环境变量和函数

(1)_G表  (个人习惯遍历下_G 看看当前环境支持什么库 很多库不是默认就支持的 需要按照流程导入或者加载)

一个全局变量(非函数),内部储存有当前所有的全局函数和全局变量的table  环境(参见 §2.2)。 Lua 自己不使用这个变量;改变这个变量的值不会对任何环境造成影响,反之亦然。(使用函数setfenv() 可以改变运行环境)

理解_G https://www.jianshu.com/p/7b8ae23ecd81

(2)_ENV表 储存这个环境(代码块)下所有能用到的全局函数和全局变量 注意在代码块定义的局部变量或者局部函数自然不会存在在这个_ENV表内 注意upvalue变量也算在里面的 用这个_ENV表可以很直观的查看 当前函数能直接调用的所有全局变量函数和upvalue变量

_ENV默认会指向_G表  _ENV表的作用:表示当前代码块的环境,每一个代码块的环境都有一张_ENV

_ENV的理解 https://www.bbsmax.com/A/nAJvmlbwzr/

(3)热更新原理了解https://www.jianshu.com/p/30416db4f649


2 lua常见的载入文件函数

https://blog.csdn.net/zxm342698145/article/details/79654623

require 加载并执行文件代码 注意加载的参数不要后缀 并且在一个页面加载同一个文件只会加载一个

dofile 加载并执行 加载参数要有后缀 每次加载都会加载 不会合成一次

loadfile 加载但是不执行代码

loadstring 加载字符串 返回一个函数  理解:f = loadstring("i = i+1")  可理解为(但不完全是)f = function()  i = i+1  end  (注:这里的变量"i"是全局变量,不是指局部变量,如果没有定义全局变量"i",调用f()则会报错!,即loadstring不涉及词法域)

简书上lua资料真的很多

2 lua其他的内置函数的了解



小知识:简易迭代器的实现思路 理解下就好

for XX in XXX do

end

当xx为nil就会停止循环

tempTbale={10,20,30,40,50,60}
--闭包思路实现一个数组迭代器
function test(T)
    --懒得检测是不是表了
    local i=0
    return function ()
                i=i+1
                return T[i],i --注意这里是返回2个值 一个是数组元素的值 第二个是数组元素的key 和平时的顺序是相反的
    end 
end

--f=test(tempTbale)

for v,k in test(tempTbale) do--这个for in 循环停止的条件是第一个参数为nil就停止
    print(v,k)
end


小知识:lua下访问网页源码 主要是涉及到socket库的使用 注意 https是无法访问的 http没问题

--socket 和socket.http 是什么关系  --》 socket.http 是socket的一个元素表  。

转载 https://bbs.xdow.net/thread-2826-1-1.html

http get请求

local http = require("socket.http")
local res, code = http.request("http://www.baidu.com");
if code == 200 then
    print(res,0);
end
--也可以这样
local response_body = {}  
local res, code = http.request({  
  url = "http://www.baidu.com",  
  sink = ltn12.sink.table(response_body)  
})

--获取外网ip地址
local http = require("socket.http")
local res, code = http.request("http://www.ip.cn/");
if code == 200 then
    local i,j = string.find(res, "%d+%.%d+%.%d+%.%d+")
    local ipaddr =string.sub(res,i,j)
    print(ipaddr,0)
end

返回的2个参数中,res 是 http body 的内容,也就是请求网页的内容,code 是 http 状态码, 返回200的话就表示正常返回。
如果传入的是 table 的话,就需要用一个容器来接收 http body 的内容。
http post请求

local http = require("socket.http")
local response_body = {}
local post_data = 'asd';  
res, code = http.request{  
    url = "http://127.0.0.1/post.php",  
    method = "POST",  
    headers =   
    {  
        ["Content-Type"] = "application/x-www-form-urlencoded",  
        ["Content-Length"] = #post_data,  
    },  
    source = ltn12.source.string('data=' .. post_data),  
    sink = ltn12.sink.table(response_body)  
}


这里注意记得 method 传入 POST, 因为默认是 GET。
headers 参数,由一个 table 组成,key 为 header,value 为 header 内容。
source 参数,这里是填入 POST 的参数,多个数据的情况用 & 隔开,例如 "data1=a&data2=b"。
此代码仅为举例说明,请勿直接复制使用。
挂载代理

--必须加上 http:// 否则不处理
local http = require("socket.http")
http.PROXY = "http://127.0.0.1:8888" --代理服务器地址
local result = http.request("http://www.baidu.com")
print(result,0)



以socket的方式访问

local http = require("socket.http")
local host = "www.baidu.com"
local file = "/"
local sock = assert(socket.connect(host, 80)) --创建一个 TCP 连接,连接到 HTTP 连接的标准 80 端口上
sock:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
repeat
    local chunk, status, partial = sock:receive(1024) --以 1K 的字节块接收数据
until status ~= "closed"
sock:close()  -- 关闭 TCP 连接

smtp方法发送mail

local smtp = require("socket.smtp")
from = "<[email protected]>" -- 发件人
--发送列表
rcpt = {
    "<[email protected]>",
    "<[email protected]>",
    "<[email protected]>",
}
mesgt = {
    headers = {
        to = "[email protected]",   -- 收件人
        cc = '<[email protected]>', -- 抄送
        subject = "This is Mail Title"
    },
    body = "这里放邮件内容"
}
r, e = smtp.send{
    server = "smtp.126.com", --smtp服务器地址
    user = "[email protected]",--smtp验证用户名
    password = "******",     --smtp验证密码  
    from = from,
    rcpt = rcpt,
    source = smtp.message(mesgt)
}
if not r then
    print(e,0);
else
    print("发送成功!"0);
end

net.time函数实现获取网络时间参考代码, 如无特殊需要请用 net.time 函数

local socket = require "socket.core"
server_ip = {
    "132.163.4.101",
    "132.163.4.102",
    "132.163.4.103",
    "128.138.140.44",
    "192.43.244.18",
    "131.107.1.10",
    "66.243.43.21",
    "216.200.93.8",
    "208.184.49.9",
    "207.126.98.204",
    "207.200.81.113",
    "205.188.185.33"
}
function nstol(str)
    assert(str and #str == 4)
    local t = {str:byte(1,-1)}
    local n = 0
    for k = 1, #t do
        n= n*256 + t[k
    end
    return n
end
function gettime(ip)
    local tcp = socket.tcp()
    tcp:settimeout(10)
    tcp:connect(ip, 37)
    success, time = pcall(nstol, tcp:receive(4))
    tcp:close()
    return success and time or nil
end
function nettime()
    for _, ip in pairs(server_ip) do
        time = gettime(ip)
        if time then
            return time
        end
    end
end
print(nettime(),0)

统计毫秒精度的时间, 如无特殊需要请用 os.clock


local socket = require ("socket")
function sleep(sec)
    socket.select(nil,nil,sec);
end
local t0 = socket.gettime()
sleep(0.4);
local t1 = socket.gettime()
print(t1 - t0,0)


小知识:lua的链表结构  和数组比较 并没有什么明显的优势 不过顺手一提

https://blog.csdn.net/thydamon/article/details/23758833


小知识:元表和元方法

主要作用是实现类的继承   基本上就是2个函数 和 几个常见的元方法

https://www.jianshu.com/p/cb945e7073a3


最基本的类继承功能的实现


zeroTable={a=1,b=2,c=3}--创建一个元表
aa={11,22,33}--普通表

zeroTable.__index=zeroTable --设置元表里面如果找不到对应索引就去后面的表去找
setmetatable(aa,zeroTable)--设置zeroTable是表aa的元表 同时元表zeroTable里面的元方法也通过这个函数到了表aa里面 注意 元表的普通元素不会过去 只有特殊的元方法才会

print(aa.a)--输出下表aa中不存在的索引项 也有值了


其他的元表元方法的使用可以看链接  感觉目前用的不多


小知识 pcall函数的基本理解

1.pcall的第一个参数是function 而且不能加括号  如果这个函数内报错 去掉错误 后面的参数其实是 第一个参数函数的参数 有点绕口

2pcall的返回值是2个 第一个返回值true false 表示里面是否无错误运行了 第二个返回函数本身的返回内容 或者 错误提示信息

test3=function (a)
    return a/2
end
traceprint(test3(4))--test23.lua:25:    2.0
traceprint(pcall(test3,6))--test23.lua:26:    true    3.0
traceprint(pcall(test3,"aaaa"))--test23.lua:27:    false    E:\lua\test23.lua:23: attempt to perform arithmetic on a string value (local 'a')

--个人理解的常见pcall的使用例子
status,result=pcall(test3,"bbb")
if status then
    traceprint("[result]:" .. tostring(result))
else
    traceprint("<error>:" .. tostring(result))--test23.lua:33:    <error>:E:\lua\test23.lua:23: attempt to perform arithmetic on a string value (local 'a')
end

猜你喜欢

转载自www.cnblogs.com/zjl8455482/p/11305017.html