GithubHelp home page GithubHelp logo

lua-resty-ip2region's Introduction

lua-resty-ip2region

根据IP地址定位所在区域的工具函数(IP数据来源于ip2region)

Overview

lua_package_path '/the/path/to/your/project/lib/?.lua';
lua_shared_dict ip_data 100m;

server {
	location =/iplocation {
		default_type text/html;
		content_by_lua_block {
			local ip2region = require 'resty.ip2region.ip2region';
			local location = iplocation:new({
			    file = "/the/path/to/your/project/lib/resty/ip2region/data/ip2region.db",
			    dict = "ip_data",
			    mode = "memory" -- maybe memory,binary or btree
			});
                        local  data = location:search('202.108.22.5');
			--[[
                             {
				country = "**",
                                    region = "华东"
				province = "浙江", 
				city = "杭州", 
				isp = "电信"
                             }
                       --]]
		}
	}
}

Methods

new

用法: ip2region_obj, err = ip2region:new({file = 'the/path/to/the/data/file', dict = 'shared dict name'})

功能:初始化ip2region模块

参数:是一个table,里面有以下选项

file:数据文件所在路径(它可以是相对目录也可以是绝对路径)

root: 项目根目录(如果file是相对目录,则要设置根目录,它默认是设置的root变量或document_root变量值)

dict:共享字典的名称(默认为ip_data,注:字典的大小建议为5m,因为文件存到内存中所占内存大约为1.5多M),

mode:查询方式(支持内存(memory)查找,二进制(binary)查找和二叉树(btree)查找)

search

用法:ip_tab,err = ip2region_obj:search(ip, multi)

功能:通过一个范围查找它的中位数以及它对应的IP数据(根据初始化参数调用相应的查询方法)

参数:

ip:查询的IP

multi:是否多次查找(多次查询不会关闭文件流,但需要手动调用close方法关闭文件流)

返回数据说明:

如果查询成功,则得到一个table数据,其结构如下:

   {
       country = "**",
       region = "华东"
       province = "浙江",
   	  city = "杭州",
   	  isp = "电信",
  
   }

字段说明:

country:为国家名称(都是中文)

region : 大区名称

province:省级行政区名称(不带行政区行政单位名称)

city:城市名称

isp:网络提供商

memory_search

用法:ip_tab,err = ip2region_obj:memory_search(ip)

功能:通过从内存数据中查找数据(如果没有对应的字典,则从数据文件中查找)

参数:

  ip:查询的IP

返回值与search方法相同

bin_search

用法:ip_tab,err = ip2region_obj:bin_search(ip)

功能:通过二进制文件查找

参数:

  ip:查询的IP

  multi:是否多次查找(多次查询不会关闭文件流,但需要手动调用close方法关闭文件流)

返回值与search方法相同

btree_search

用法:ip_tab,err = ip2region_obj:btree_search(ip)

功能:通过btree方法在文件中查找

参数:

  ip:查询的IP

  multi:是否多次查找(多次查询不会关闭文件流,但需要手动调用close方法关闭文件流)

返回值与search方法相同

loadfile

用法:content, err = ip2region_obj:loadfile()

功能:加载数据文件并返回数据文件中的数据(内部使用)

close

用法:ip2region_obj:close()

功能:关闭文件流

TODO

数据文件进一步加工(返回更多的字段)

contact

也请各位同学反馈bug

E-mail:[email protected]

website:www.shixinke.com

lua-resty-ip2region's People

Contributors

shixinke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.