使用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)