使用Lua的擴充套件庫LuaSocket用例
阿新 • • 發佈:2020-08-15
LuaSocket 是 Lua 的網路模組庫,它可以很方便地提供 TCP、UDP、DNS、FTP、HTTP、SMTP、MIME 等多種網路協議的訪問操作。 它由兩部分組成:一部分是用 C 寫的核心,提供對 TCP 和 UDP 傳輸層的訪問支援。另外一部分是用 Lua 寫的,負責應用功能的網路介面處理。 一、安裝LuaSocket 下面介紹兩種安裝方法 第一種方法:如果你有安裝了 Lua 模組的安裝和部署工具 LuaRocks,那麼一條指令就能安裝部署好 LuaSocket: # luarocks install luasocket 第二種方法:如果沒安裝有 LuaRocks,也可以原始碼安裝。 先把 LuaRocks 下載下來,當前可用的版本是 luasocket-3.0-rc1(luasocket的原始碼有託管在Github.com): # git clone https://github.com/diegonehab/luasocket.git 把原始碼clone下來之後就可以進行本地原始碼安裝,直接進入到luasocket目錄進行編譯安裝了 # cd luasocket # make && make install LuaSocket 使用 接下來是LuaSocket擴充套件的幾種使用方法 1、socket方式請求 -- socket方式請求 local socket = require("socket") local host = "100.42.237.125" 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 的位元組塊來接收資料,並把接收到位元組塊輸出來 -- print(chunk or partial) until status ~= "closed" sock:close() -- 關閉 TCP 連線 2、HTTP訪問請求 -- http訪問請求 http=require("socket.http") result=http.request("http://ip.taobao.com/service/getIpInfo.php?ip=123.189.1.100") print(result) 3、SMTP方法傳送mail -- smtp方法傳送mail local smtp = require("socket.smtp") from = "<[email protected]>" -- 發件人 -- 傳送列表 rcpt = { "<[email protected]>", "<[email protected]>" } mesgt = { headers = { to = "[email protected]", -- 收件人 cc = '<[email protected]>', -- 抄送 subject = "This is Mail Title" }, body = "This is Mail Content." } r, e = smtp.send{ server="smtp.126.com", user="[email protected]", password="******", from = from, rcpt = rcpt, source = smtp.message(mesgt) } if not r then print(e) else print("send ok!") end 使用 LuaSocket 還算簡單吧,直接用 require 函式載入進來就行,在例如下面幾個例子 1)輸出一個 LuaSocket 版本資訊: local socket = require("socket") print(socket._VERSION) 2)以 socket 的方式訪問獲取百度首頁資料: local socket = require("socket") local host = "www.baidu.com" local file = "/" -- 建立一個 TCP 連線,連線到 HTTP 連線的標準埠 -- 80 埠上 local sock = assert(socket.connect(host, 80)) sock:send("GET " .. file .. " HTTP/1.0\r\n\r\n") repeat -- 以 1K 的位元組塊來接收資料,並把接收到位元組塊輸出來 local chunk, status, partial = sock:receive(1024) print(chunk or partial) until status ~= "closed" -- 關閉 TCP 連線 sock:close() 3)使用模組裡內建的 http 方法來訪問: local http = require("socket.http") local response = http.request("http://www.baidu.com/") print(response) 一個簡單的 client/server 通訊連線 本來想寫成單 server 多 client 的 socket 聊天伺服器,不過最後還是卡在客戶端的資料更新上,單程序的 while 輪詢(poll),一個 io.read 就把伺服器資料接收給截斷了。 僅靠現有的 LuaSocket 模組不裝其他第三方模組,也是很難做一個實時的聊天,雖然有 soket.select 在苦苦支撐,但是這還是一個填不平的坑來了。 可能用上面向併發的 concurrentlua 模組會解決這個資料接收阻塞問題,這個以後再看看,現階段的成果是:在客戶端的終端上敲一些東西后回車會通過 socket 給伺服器傳送資料,伺服器接收到資料後再返回顯示在客戶端的終端上。 一個簡單的東西,純屬練手,程式碼如下: server端 -- server.lua local socket = require("socket") local host = "127.0.0.1" local port = "12345" local server = assert(socket.bind(host, port, 1024)) server:settimeout(0) local client_tab = {} local conn_count = 0 print("Server Start " .. host .. ":" .. port) while 1 do local conn = server:accept() if conn then conn_count = conn_count + 1 client_tab[conn_count] = conn print("A client successfully connect!") end for conn_count, client in pairs(client_tab) do local recvt, sendt, status = socket.select({client}, nil, 1) if #recvt > 0 then local receive, receive_status = client:receive() if receive_status ~= "closed" then if receive then assert(client:send("Client " .. conn_count .. " Send : ")) assert(client:send(receive .. "\n")) print("Receive Client " .. conn_count .. " : ", receive) end else table.remove(client_tab, conn_count) client:close() print("Client " .. conn_count .. " disconnect!") end end end end client端 -- client.lua local socket = require("socket") local host = "127.0.0.1" local port = 12345 local sock = assert(socket.connect(host, port)) sock:settimeout(0) print("Press enter after input something:") local input, recvt, sendt, status while true do input = io.read() if #input > 0 then assert(sock:send(input .. "\n")) end recvt, sendt, status = socket.select({sock}, nil, 1) while #recvt > 0 do local response, receive_status = sock:receive() if receive_status ~= "closed" then if response then print(response) recvt, sendt, status = socket.select({sock}, nil, 1) end else break end end end 轉載:D.H.Q的爛筆頭https://www.cnblogs.com/archoncap/p/5238229.html