触动精灵远程Log模块
一、功能
lua log方法能够自动发现同一网段下面的log服务器
lua log方法能够主动将log发给服务器
lua 客户端进程重启服务端不存在影响
二、实现
服务器使用python编写:
启动一个线程,用UDP监听特定端口,接受客户端的扫描,反馈日志的端口
启动一个线程,监听特定端口,接受TCP连接,分派新的日志线程处理log
客户端使用lua 编写:
打印log之前使用UDP套节字扫描服务器的日志端口,然后创建TCP套节字发送日志
三、源代码:
服务器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import socket import time import re import thread udp_listen_port = 31500 tcp_listen_port = 31501 stop_udp_server = False def getLocalIP(): myname = socket.getfqdn(socket.gethostname()) myaddr = socket.gethostbyname(myname) return myaddr; def getTime(): return time.strftime( '%Y-%m-%d %H:%M:%S' ,time.localtime(time.time())) def Log(content): print ( "[%s]: %s" % (getTime() ,content)) def startUDPServer(): address = (getLocalIP(), udp_listen_port) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(address) print ( 'listen broadcast %s:%s' % address) Log( 'listen broadcast %s:%s' % address) while not stop_udp_server: data, addr = s.recvfrom( 2048 ) if not data: Log( "client not existed!" ) continue Log( 'received %s from %s' % (data, addr)) client_ip_port = '{0}:{1}' . format (addr[ 0 ],addr[ 1 ]) pat = re. compile ( "^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d+)$" ) for ip,port in pat.findall(client_ip_port): Log( 'Got iphone addr:port = %s %s' % (ip, port)) local_addr_port = (getLocalIP(), tcp_listen_port) Log( 'Rsp server addr:port = %s %s' % local_addr_port) rsp_data = '{0}' . format (local_addr_port[ 1 ]) s.sendto(rsp_data,addr) break ; s.close() def startTCPLogServer(sock,client): while ( True ): try : data = sock.recv( 4096 ) if data: pat = re. compile ( '\[(.*?)\]' ) for log in pat.findall(data): Log(log) else : break ; except socket.error: sock.close() break Log( 'client (%s,%s) close' % client); sock.close() def startTCPListenServer(): #create tcp socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #bind local ip-port server_addr = (getLocalIP(), tcp_listen_port) Log( 'start tcp server on %s port %s' % server_addr) sock.bind(server_addr) sock.listen( 5 ) #print recv data while ( True ): ss,addr = sock.accept() Log( 'Got log client from (%s,%s)' % addr) thread.start_new_thread(startTCPLogServer,(ss,addr,)) sock.close() if __name__ = = '__main__' : t_udp = thread.start_new_thread(startUDPServer, ()) t_tcp = thread.start_new_thread(startTCPListenServer, ()) while ( True ): time.sleep( 1 ) |
客户端:
udp_listen_port = 31500 tcp_server_ip = "" tcp_server_port = 0 function getLocalIP() local socket = require("socket") local ip, v = socket.dns.toip(socket.dns.gethostname()) for k,v in pairs(v.ip) do return v end return nil end function tryFindLogServer() local ip = getLocalIP(); local ipTable = {} local socket = require("socket") local udp = socket.udp() if ip ~= nil then for i in string.gmatch(ip, "%d+") do print(i) table.insert(ipTable,i) end end local length = table.getn(ipTable) local findip = nil local findport = nil if length > 0 then for i = 100, 255 do local desIP = string.format("%s.%s.%s.%s",ipTable[1], ipTable[2], ipTable[3], tostring(i)) print(desIP) udp:setpeername(desIP, udp_listen_port) udp:settimeout(0.5) udp:send("Hello") data = udp:receive() if data ~= nil then print(string.format("find server %s:%s", desIP,data)) tcp_server_ip = desIP tcp_server_port = tonumber(data) return true; end end end return false end tcp = nil function Log( content ) -- body if tcp == nil then local socket = require("socket") tcp = socket.tcp() tcp:connect(tcp_server_ip,tcp_server_port) end tcp:send(string.format("[%s]",tostring(content))) end if tryFindLogServer() then for i = 1,100 do Log("find server") end end
四、客户端lua中加载luasocket的方式如下:
local sz = require("sz") local http = require("szocket.http") local res, code = http.request("http://www.baidu.com"); if code == 200 then Log("start") Log(res) dialog(res,0); Log("end") end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架