Internet(因特网) API

此库封装了因特网卡的功能。
另请参阅因特网组件以了解更多底层功能(例如查询HTTP可用性以及TCP功能)。

  • internet.request(url: string[, data: string or table[, headers: table[, method: string]]]): function
    向指定URL发送带有POST数据(如果有)的HTTP请求。如果没有POST数据,则会发送GET请求。
    POST数据有两种格式可选:字符串和表。如果是字符串,则按原样发送。如果是表,它将被转换为字符串,方式是将各个键作为POST变量的名称,其对应值为该变量的值。
    method参数可以显式指定为GET、POST或PUT等值。
    一些样例:
    • internet.request(url, {some = "variable", another = 1})会发送 some=variable&another=1
    • 指定使用PUT的样例:internet.request("https://example.com", "put data", {}, "PUT")

    返回的函数是对结果块的迭代器,使用方式:for chunk in internet.request(...) do stuff() end
    注意,此方法也支持HTTPS。所以只需要用internet.request("https://example.com")即可通过HTTPS发送请求。

  • internet.socket(address:string[, port:number]):table
    使用因特网组件的connect方法打开一个TCP套接字,并将其封装为一个表,此表提供了与使用filesystem.open打开的文件相同的方法:readwriteclose(还有seek,但通常会执行失败)。我们更推荐使用internet.open函数,此函数会将打开的套接字封装进缓冲,与 io.open 函数封装文件的方式相同。
    返回的套接字上的读取方法是非阻塞的。读取结果会立刻返回,但是若没有内容可读取则可能返回空字符串。写入操作可能会被阻塞,直到所有数据均被成功写入,但是通常而言会立刻返回。

  • internet.open(address:string[, port:number]):table
    打开一个到指定地址的带缓冲套接字流。此流可读可写,分别使用s:reads:write方法——通常可以与io.open打开的文件类似对待。通常需要使用s:setTimeout(seconds)设定缓冲区读取超时时间,以防止操作被无限期阻塞。
    返回的缓冲区上的读取操作是阻塞式的。读取操作会等待有可读取的数据出现,并将其返回。
    使用例:
snippet.lua
local internet = require("internet")  
local handle = internet.open("example.com", 1337)  
local data = handle:read(10)  
handle:write("1234")  
handle:close()  

如果你需要HTTP响应码、消息和响应头,这些数据可从内部对象中取回。该对象被存储于返回对象的元表中。

snippet.lua
-- https://github.com/kikito/inspect.lua/blob/master/inspect.lua
local inspect = require("inspect")
local internet = require("internet")
 
local handle = internet.request("https://www.google.com")
local result = ""
for chunk in handle do result = result..chunk end
-- 输出HTTP响应的body
-- print(result)
 
-- 从句柄处取得元表,其中包含内部的HTTPRequest对象
local mt = getmetatable(handle)
 
-- response方法取得了HTTP响应码、响应信息和响应头的信息
local code, message, headers = mt.__index.response()
print("code = "..tostring(code))
print("message = "..tostring(message))
print(inspect(headers))

下面提供了一个简单的IRC聊天机器人样例,机器人会复读你说的话。样例使用了internet(因特网) API提供的套接字。

snippet.lua
--这是一个简易的拆分函数,用于拆分信息
function split(data, pat)
	local ret = {}
	for i in string.gmatch(data,pat) do
		table.insert(ret,i)
	end
	return ret
end
--配置
local nickname = "myircbot"
local channel = "#mybotchannel"
 
local net = require("internet")
local con = net.open("irc.esper.net",6667) --在此处设定服务器与端口,将会连接到服务器
if(con) then
	local line,png,linesplt,msgfrom = ""
	while(true) do
		line = con:read() --从套接字中读取一行
		print(line)
		linesplt = split(line,"[^:]+")
		if #linesplt >= 2 and string.find(linesplt[2], "No Ident response") ~= nil then
			print("JOIN")
			con:write("USER " .. nickname .. " 0 * :" .. nickname .. "\r\n") --con:write(msg)用于发送信息,con:read()会读取一行
			con:write("NICK " .. nickname .. "\r\n") --对IRC而言,请记住在所有信息末尾追加\r\n
			con:write("JOIN :" .. channel .. "\r\n")
		elseif linesplt[1] == "PING" or linesplt[1] == "PING " then
			print("PING")
			png = split(line,"[^:]+")
			con:write("PONG :"..png[#png].."\r\n") --对ping作出响应,以保证不会断联
		elseif string.find(linesplt[1], "PRIVMSG #") ~= nil then
			msgfrom = split(linesplt[1],"[^ ]+")
			msgfrom = msgfrom[3]
			con:write("PRIVMSG "..msgfrom.." :"..linesplt[2].."\r\n")
		end
	end
else
	print("Connection failed.")
end

若要获取更高级的使用例,请查看最新版本的OpenComputers提供的IRC客户端程序:irc.lua

目录