检索客户端的IP地址(或尽最大努力)
sanic-ipware的Python项目详细描述
这是与Sanic一起工作的django-ipware叉。
概述
在保持客户端的IP地址dry的同时,尝试获取客户端的IP地址。
注意
没有真正好的“开箱即用”的解决方案来对付假IP地址,也就是“IP地址欺骗”。我们鼓励您阅读本页的Advanced users部分,并使用trusted_proxies_ips和/或proxy_count功能来满足您的需要,特别是如果您计划在任何与身份验证、安全或“反欺诈”相关的体系结构中包含sanic-ipware。
如何安装
安装sanic-ipware的最佳方法是使用pip:
pip install sanic-ipware
如何使用
基本上有一个方法可以从sanic_ipware使用,叫做get_client_ip。结果是Tuple[Optional[str], bool]的(ipaddr, routable)。
fromsanic_ipwareimportget_client_ip@app.get("/some/handler")asyncdefsomehandler(request):ip,routable=get_client_ip(request)ifipisnotNone:ifroutable:# we have a (probably) real, public ip address for userelse:# we have ip address, but it might not be public routableelse:# we don't have a ip address for the user
高级用户
# you can provide your own meta precedence order by using the# request_header_order in the function call:ip,routable=get_client_ip(request,request_header_order=['Forwarded-For','X-Forwarded-For'])# if you're going to do this a lot, wrap the function somewhere with# functools.partialfromfunctoolsimportpartialmy_get_client_ip=partial(get_client_ip,request_header_order=['Forwarded-For','X-Forwarded-For'])ip,routable=my_get_client_ip(request)# if you plan to use sanic_ipware in any authentication, security or# "anti-fraud" related architecture, you should configure it to only# "trust" one or more "known" proxy server(s)), in the function call:ip,routable=get_client_ip(request,proxy_trusted_ips=['198.84.193.158'])# you can perform the same functools.partial trick with these trusted IPs
2018年9月28日v0.1.0
- pypi上的第一个版本。不稳定。