Ubuntu / Openwrt使用lua发送http和https请求
- 1、Ubuntu配置以支持lua发送http和https请求
- 1.1、配置apt镜像源
- 1.2、安装相关lua关联包
- 2、Openwrt配置menuconfig支持lua发送http和https请求
- 2.1、配置menuconfig
- 3、Ubuntu / Openwrt 使用lua发送http和https请求
- 3.1、测试发送http请求
- 3.2、测试发送https请求
1、Ubuntu配置以支持lua发送http和https请求
1.1、配置apt镜像源
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 6.5.0-41-generic x86_64)
链接: https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.6c1a1b117VwAM7
sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak将下面的ubuntu 22.04 LTS (jammy) 配置配置到这个源文件中
sudo mv /etc/apt/sources.list
ubuntu 22.04 LTS (jammy) 配置如下
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse# deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
1.2、安装相关lua关联包
sudo apt update
sudo apt install -y lua5.3
lua -v
sudo apt install -y liblua5.3-devsudo apt install -y luarocks
sudo luarocks install luasocket
2、Openwrt配置menuconfig支持lua发送http和https请求
2.1、配置menuconfig
配置选中 Languages -> Lua -> <*> luarocks
以支持https。
3、Ubuntu / Openwrt 使用lua发送http和https请求
3.1、测试发送http请求
local http = require("socket.http")local url = "http://data-studio.gds.org.cn/Data/NewGtin"
local resp, code, headers, status = http.request(url)if code == 200 thenprint(resp)
elseprint("Request failed with status code: " .. code)
end
3.2、测试发送https请求
local http = require "socket.http"
local ssl = require "ssl.https"local url = "https://www.baidu.com"
local headers = {}-- 设置 HTTPS 选项
local options = {protocol = "TLSv1_2",verify = false,
}-- 发送 HTTPS GET 请求
local response, status_code, response_headers = ssl.request({method = "GET",url = url,headers = headers,options = options,
})if status_code == 200 thenprint(response)print(status_code)print(response_headers )
elseprint("Request failed with status code: " .. status_code)
end