欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > Ubuntu / Openwrt使用lua发送http和https请求

Ubuntu / Openwrt使用lua发送http和https请求

2024/10/24 10:14:22 来源:https://blog.csdn.net/Michael_lcf/article/details/140150751  浏览:    关键词:Ubuntu / Openwrt使用lua发送http和https请求

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com