【问题标题】:Get client ip on server side with lua使用lua在服务器端获取客户端IP
【发布时间】:2015-05-21 19:58:16
【问题描述】:

我想知道连接到我的 lua 服务器的客户端 ip,以确定连接是来自 Internet 还是 Intranet。

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
  print(conn)
  conn:on("receive",function(conn,payload)
    ip = conn:getpeername()
    print(ip)
    conn:send("<h1> TEST.</h1>")
  end)
  conn:on("sent",function(conn) conn:close() end)
end)

我得到错误:

PANIC: unprotected error in call to Lua API (stdin:2: attempt to call method 'getpeername' (a nil value))

注意:我在 esp8266 芯片上使用这个,我没有使用 lua 的经验!

如何找到连接的客户端的IP?

【问题讨论】:

  • 你使用哪个库来建立连接?
  • 我不知道...必须在 nodemcu-firmware github.com/nodemcu/nodemcu-firmware 中实现,但它说:基于 Lua 5.1.4(没有调试,os 模块。)
  • 不要在回调函数中使用相同的变量名。
  • 你的解决方案不能解决我的问题
  • 如果找不到文档,请尝试探索 API 详细信息。 for k,v in pairs(conn) do print(k,v) end

标签: tcp lua ip esp8266


【解决方案1】:

使用新补丁:ip,port = conn:getpeer() 有效。

【讨论】:

  • 可能是切换了,但是当前返回集是port, ip
猜你喜欢
  • 1970-01-01
  • 2018-12-30
  • 2010-11-16
  • 2019-01-16
  • 2017-08-04
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多