This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
api:internet:zh [2023/06/08 10:57] whitenx [Internet API] |
api:internet:zh [2023/10/19 10:28] hfsr [Internet API] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Internet API | + | Internet(因特网) API |
========== | ========== | ||
- | + | 此库包含因特网卡的功能。另请参阅[[component:internet|因特网组件]]以了解更多底层功能 | |
- | **本篇文章的部分/全部内容还没有进行翻译。** | + | (例如查询HTTP可用性与TCP功能)。 |
- | + | ||
- | 此库包含因特网卡的功能。另请参阅[[component:internet|因特网组件]]以了解更多功能 | + | |
- | (例如查询HTTP和TCP功能的可用性)。 | + | |
- `internet.request(url: string[, data: string or table[, headers: table[, method: string]]]): function` | - `internet.request(url: string[, data: string or table[, headers: table[, method: string]]]): function` | ||
- | 向指定URL发送带有POST数据(如果有)的HTTP请求。如果没有POST数据,则将会发送GET请求。POST数据具有两种格式:字符串和表。如果是字符串,则按原样发送。如果它是一个表,它将被转换为一个字符串,假设每个键是一个POST变量的名称,它的相关值是该变量的值。方法可以显式指定为GET、POST或PUT等值。比如: | + | 向指定URL发送带有POST数据(如果有)的HTTP请求。如果没有POST数据,则将会发送GET请求。POST数据有两种格式可选:字符串和表。如果是字符串,则按原样发送。如果是一个表,它将被转换为一个字符串,假设每个键都是一个POST变量的名称,其对应值是该变量的值。方法可以显式指定为GET、POST或PUT等值。比如: |
`internet.request(url, {some = "variable", another = 1})` | `internet.request(url, {some = "variable", another = 1})` | ||
会发送 `some=variable&another=1`. | 会发送 `some=variable&another=1`. | ||
Line 20: | Line 17: | ||
- `internet.socket(address:string[, port:number]):table` | - `internet.socket(address:string[, port:number]):table` | ||
- | Opens a TCP socket using an internet component's `connect` method and wraps it in a table that provides the same methods as a file opened using `filesystem.open`: `read`, `write` and `close` (and `seek`, which will always fail). It is recommended to use `internet.open` instead, which will wrap the opened socket in a [[api:buffer|buffer]], the same way `io.open` wraps files. | + | 使用因特网组件的`connect`方法打开一个TCP套接字,并将其包装为一个表,表中提供了与使用 `filesystem.open` 打开的文件相同的方法: `read`、`write`与`close`(还有`seek`,但通常会执行失败)。我们更推荐使用 `internet.open` 函数,此函数会将打开的套接字包装上[[api:buffer:zh|缓冲]],与 `io.open` 函数包装文件的方式相同。 |
- | The read method on the returned socket is *non-blocking*. Read will instantly return, but may return an empty string if there is nothing to read. Write *may* block until all data has been successfully written. It'll *usually* return immediately, though. | + | 返回的套接字上的读取方法是*非阻塞*的。读取结果会立刻返回,但是若没有内容可读取则可能返回空字符串。写入操作*可能会*被阻塞,直到所有数据均被成功写入,但是*通常而言*会立刻返回。 |
- `internet.open(address:string[, port:number]):table` | - `internet.open(address:string[, port:number]):table` | ||
- | Opens a buffered socket stream to the specified address. The stream can be read from and written from, using `s:read` and `s:write` - in general it can be treated much like files opened using `io.open`. It may often be desirable to set the buffer's read timeout using `s:setTimeout(seconds)`, to avoid it blocking indefinitely. | + | 打开一个到指定地址的带缓冲套接字流。此流可读可写,只需使用 `s:read` 和 `s:write` 方法——通常可以与 `io.open` 打开的文件类似对待。通常需要使用`s:setTimeout(seconds)`设定缓冲区读取超时时间,以防止操作被无限期阻塞。 |
- | The read method on the returned buffer is *blocking*. Read will wait until some data is available to be read and return that. | + | 返回的缓冲区上的读取操作是*阻塞式*的。读取操作会等待有可读取的数据出现,并将其返回。 |
- | Example usage: | + | 使用例: |
```lua | ```lua | ||
Line 34: | Line 31: | ||
handle:close() | handle:close() | ||
``` | ``` | ||
- | If you need the HTTP response code, message, and headers, they are retrieved from the internal object, which is stored in the metatable of the returned object. | + | 如果你需要HTTP响应码、消息和响应头,这些数据可从内部对象中取回。该对象被存储于返回对象的元表中。 |
```lua | ```lua | ||
-- https://github.com/kikito/inspect.lua/blob/master/inspect.lua | -- https://github.com/kikito/inspect.lua/blob/master/inspect.lua | ||
Line 43: | Line 40: | ||
local result = "" | local result = "" | ||
for chunk in handle do result = result..chunk end | for chunk in handle do result = result..chunk end | ||
- | -- Print the body of the HTTP response | + | -- 输出HTTP响应的body |
-- print(result) | -- print(result) | ||
- | -- Grab the metatable for the handle. This contains the | + | -- 从句柄处取得元表,其中包含内部的HTTPRequest对象 |
- | -- internal HTTPRequest object. | + | |
local mt = getmetatable(handle) | local mt = getmetatable(handle) | ||
- | -- The response method grabs the information for | + | -- response方法取得了HTTP响应码、响应信息和响应头的信息 |
- | -- the HTTP response code, the response message, and the | + | |
- | -- response headers. | + | |
local code, message, headers = mt.__index.response() | local code, message, headers = mt.__index.response() | ||
print("code = "..tostring(code)) | print("code = "..tostring(code)) | ||
Line 59: | Line 53: | ||
``` | ``` | ||
- | This is an example of a basic IRC bot that echos back what you say to it, using the sockets in the internet api. | + | 下面提供了一个简单的IRC聊天机器人样例,机器人会复读你说的话。样例使用了internet(因特网) api提供的套接字。 |
```lua | ```lua | ||
- | --this is just a basic split function we'll use to split the messages | + | --这是一个简易的拆分函数,用于拆分信息 |
function split(data, pat) | function split(data, pat) | ||
local ret = {} | local ret = {} | ||
Line 69: | Line 63: | ||
return ret | return ret | ||
end | end | ||
- | --config | + | --配置 |
local nickname = "myircbot" | local nickname = "myircbot" | ||
local channel = "#mybotchannel" | local channel = "#mybotchannel" | ||
Line 101: | Line 95: | ||
``` | ``` | ||
- | For a more advanced example, check out the IRC Client program available in the latest release of OpenComputers: [irc.lua](https://github.com/MightyPirates/OpenComputers/blob/master-MC1.7.10/src/main/resources/assets/opencomputers/loot/irc/usr/bin/irc.lua) | + | 若要获取更高级的使用例,请查看最新版本的OpenComputers提供的IRC客户端程序:[irc.lua](https://github.com/MightyPirates/OpenComputers/blob/master-MC1.7.10/src/main/resources/assets/opencomputers/loot/irc/usr/bin/irc.lua) |
目录 | 目录 |