简体   繁体   English

从Delicious的api调用中读取IP地址?

[英]Read IP address from api call in Tastypie?

In my code i want to know the ip address of the client from which the api call is made. 在我的代码中,我想知道从中进行api调用的客户端的IP地址。 Is there any way? 有什么办法吗?

eg: Assume some guy initiated this api call like "http://server_url/api/v1/range/setup/" 例如:假设有人发起了此api调用,例如"http://server_url/api/v1/range/setup/"

In my api.py in server can i read the ip address of his machine ? 在服务器的api.py中,我可以读取他的机器的IP地址吗?

I once used this method, which should return IP address from your request meta data: 我曾经使用过这种方法,该方法应该从您的请求元数据中返回IP地址:

def getClientIPaddress(request):
    http_x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if http_x_forwarded_for:
        ip_address = http_x_forwarded_for.split(',')[0]
    else:
        ip_address = request.META.get('REMOTE_ADDR')
    return ip_address

Then you can call getClientIPaddress method from whereever you process your API call, eg: 然后,您可以从处理API调用的任何地方调用getClientIPaddress方法,例如:

class YourResource(ModelResource):
    class Meta:
        #meta code here

    def obj_create(self, bundle, **kwargs):
        ip = getClientIPaddress(bundle.request)
        #your code here

    def obj_get_list(self, bundle, **kwargs):
        ip = getClientIPaddress(bundle.request)
        #your code here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM