当前位置: 首页 > 编程笔记 >

openresty中使用lua-nginx创建socket实例

卢景澄
2023-03-14
本文向大家介绍openresty中使用lua-nginx创建socket实例,包括了openresty中使用lua-nginx创建socket实例的使用技巧和注意事项,需要的朋友参考一下

Lua语言太强大了,至少我是这样觉得的。原始的Lua没有Socket功能,需要使用者下载Lua socket组件,require一下才行。而lua-nginx模块自带了socket功能,而且是100%的非阻塞模式,再次感谢作者章亦春。

使用socket功能很简单,只有几个简单的方法即可主要就是有TCP和UDP的区别。(这里只是lua文件,其他请见Hello world 文章)


local sock = ngx.socket.tcp()

local ok,err = sock:connect('whois.cnnic.net.cn',43)

if not ok then

 ngx.say('Failed to connect whois server',err)

 return

end

sock:settimeout(5000)

local ok, err = sock:send("baidu.cn\r\n")

if not ok then

 ngx.say('Failed to send data to whois server', err)

 return

end

local line, err, partial = sock:receive('*a')

if not line then

 ngx.say('Failed to read a line', err)

 return

end

ngx.print(line)


完美运行:


 类似资料:
  • 简介 Botsonar 一款企业使用的反爬虫管理平台。该平台集爬虫发现,策略,防御,流量分析于一体,目前处于 Alpha 测试版本,开源测试版本为旁路分析模式。 站点概览 防御和威胁分类 攻击 IP 详情 访问路径 路径威胁详情 特性 全站流量统计 IP 攻击分类 搜索引擎自动放行 威胁类型分类 验证码推送支持 IP、Country、UA 策略 会话频率限制 Javascript 检查 黑产 IP

  • nginx openresty 迁移到 windows 下的版本。  by 张家柱 蔡东赟  韩玉刚(new) 主要特性: 高性能 超过 20,000 连接并发 (特定cpu多核的机器上,server新版本的 190,000并发) 多进程 各种 Nginx bug 修复 包括以下模块: ngx-coolkit-module echo-nginx-module set-misc-nginx-modu

  • 本文向大家介绍Lua 元表的创建和使用,包括了Lua 元表的创建和使用的使用技巧和注意事项,需要的朋友参考一下 示例 一个元表定义了一组改变lua对象行为的操作。一个元表只是一个普通的表,它以一种特殊的方式使用。            

  • 本文向大家介绍nginx中使用lua脚本的方法,包括了nginx中使用lua脚本的方法的使用技巧和注意事项,需要的朋友参考一下 Lua是一种跟JavaScript很像的语言,Ngix_Lua同样使用异步单线程,语法甚至比JS更加简单,之前的评测指出,Ngix_lua的性能几乎是Node.JS的一倍。 Nginx 特点 1.流行的高性能HTTP服务器 2.事件驱动(异步)架构 3.少量且可测内存占用

  • This Lua library implements both a nonblocking WebSocket server and a nonblocking WebSocket client based on Lua Nginx Module's cosocket API. Project homepage: https://github.com/agentzh/lua-resty-webs

  • This module embeds the Lua interpreter or LuaJIT into the nginx core and integrates the powerful Lua threads (aka Lua coroutines) into the nginx event model by means of nginx subrequests. Unlike Apach

  • Nginx与Lua编写脚本的基本构建块是指令执行顺序的图 Nginx 教程 基础 Nginx编译安装 Nginx.conf详解 Location 详解 Nginx基础知识 Nginx高性能WEB服务器详解 Nginx高并发系统内核优化和PHP7配置文件优化 Nginx和PHP-FPM启动脚本 Nginx的11个Phases agentzh 的 Nginx 教程 Nginx 陷阱和常见错误 TCP和

  • lua-nginx-module (ngx_lua) 可在 Nginx 中嵌入 Lua 语言。让 Nginx 可以支持 Lua 强大的语法。 概要: # set search paths for pure Lua external libraries (';;' is the default path): lua_package_path '/foo/bar/?.lua;/blah/?.lua;