lua安装LuaSocket实现CURL的GET/POST请求
Song •
4198 次浏览 •
0个评论 •
2018年09月11日
安装luarocks
LuaRocks
是Lua
模块的包管理器。这里只介绍ubuntu/debian
安装luarocks
,其它系统类似
apt-get install luarocks
安装LuaSocket
安装LuaSocket
有两种方法,源码安装和luarocks
安装。这只介绍luarocks
的安装方法,其他的可以查看源码安装luasocket
luarocks install luasocket
LuaSocket的使用
接下来介绍一下LuaSocket
的基本使用
1、输出一个 LuaSocket 版本信息
local socket = require("socket")
print(socket._VERSION)
2、发送HTTP请求
我们来发送http
请求
-- http访问请求
http=require("socket.http")
result=http.request("http://ip.taobao.com/service/getIpInfo.php?ip=123.189.1.100")
print(result)
如果要发送post
请求
http.request{
url = string,
[sink = LTN12 sink,]
[method = string,]
[headers = header-table,]
[source = LTN12 source],
[step = LTN12 pump step,]
[proxy = string,]
[redirect = boolean,]
[create = function]
}
method
:HTTP
请求方法。默认为GET
;headers
:随请求一起发送的任何其他HTTP
头;source
:”请求标头字段,否则该函数将尝试将主体发送为“ chunked ”(少数服务器支持的东西)。默认为空源;step
:LTN12
步功能用于移动数据。默认为LTN12 pump.step
功能。proxy
:要使用的代理服务器的URL
。默认为无代理;redirect
:设置为false
以防止该功能自动跟踪301
或302
服务器重定向消息;create
:创建通信套接字时使用的可选函数,而不是socket.tcp。
例子:
#!/usr/bin/env lua
local http=require("socket.http");
local ltn12 = require("ltn12");
local request_body = [[login=user&password=123]]
local response_body = {}
local res, code, response_headers = http.request{
url = "http://httpbin.org/post",
method = "POST",
headers =
{
["Content-Type"] = "application/x-www-form-urlencoded";
["Content-Length"] = #request_body;
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
}
print(res)
print(code)
if type(response_headers) == "table" then
for k, v in pairs(response_headers) do
print(k, v)
end
end
print("Response body:")
if type(response_body) == "table" then
print(table.concat(response_body))
else
print("Not a table:", type(response_body))
end
- 官方文档:LuaSocket
- 更多参考:源码安装luasocket
用户评论
当前暂无评价,快来发表您的观点吧...
更多相关好文
当前暂无更多相关好文推荐...
-
微信公众号文章/菜单添加小程序时路径如何获取? 2021-12-22
-
如何轻松获取微信小程序路径path? 2021-12-22
-
cannot import name 'CUDA_HOME' from 'mmcv.utils' 2021-12-05
-
vgg的loss一轮达到ln(1/n)阈值,如何解决 2021-11-21
-
如何下载使用utils库 2021-10-27
热门文章
-
微信公众号文章/菜单添加小程序时路径如何获取? 2021-12-22
-
如何轻松获取微信小程序路径path? 2021-12-22
-
python/MySQL分页查询方法与性能优化 2021-06-23
-
mitmproxy & python 忽略所有的https/ssl请求 2021-04-19
-
如何使用邮件/邮箱推广微信公众号/小程序? 2021-01-28
栏目最新文章
公告提示
- pytorch中文文档
- pytorch官方文档
提交评论