Skip to Content
Menu
This question has been flagged
7 Replies
11021 Views

i was create new module on OpenERP 7 and this module must save user IP Address, how can i get the user IP Address not server IP Address?

 

below my code and it give error "NameError: global name 'os' is not defined"

_columns = { 'ip_address': fields.function(_calculate_ipaddress,type='char',string='IP Address',store=True), }

def _calculate_ipaddress(self,cr,uid,ids,fields,args,context={}):

    res = {}

    for at in self.browse(cr,uid,ids,context=context):

        res[at.id] = os.environ["REMOTE_ADDR"]

    return res

Avatar
Discard

You can get the user his IP address in python with os.environ["REMOTE_ADDR"] so I assume you should trigger a function and then store the address somewhere.

Author

thanks for reply.. i get this error: NameError: global name 'os' is not defined i wont to save it on function field _columns = { 'ip_address': fields.function(_calculate_ipaddress,type='char',string='IP Address',store=True), } def _calculate_ipaddress(self,cr,uid,ids,fields,args,context={}): res = {} for at in self.browse(cr,uid,ids,context=context): res[at.id] = os.environ["REMOTE_ADDR"] return res

Best Answer

hi Mohammad ,

you try this code to get user's IP Address

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("gmail.com",80))
print(s.getsockname()[0])
s.close()

Avatar
Discard
Best Answer

The os.environ["REMOTE_ADDR"] will contain the IP address of the user only in case of CGI call. In case of odoo (openerp), the server will start in WSGI mode, in which case the IP address will be in the environ attribute attached to the request object passed to the server. Unfortunatly, there is no simple way to access the request object since it is not part of the global variables. One workaround would be to wrap the wsgi_handler method in wsgi_server.py to intercept the environ variable pass to the method from which you can read the REMOTE_ADDR and store it in a global variable. Once you have this, you can access the global variable from the methods in your model.

Avatar
Discard
Best Answer

Use the following chunk of code , change it according to your need

USER_PRIVATE_FIELDS = ['password']


@api.model
def _check_credentials(self, password):
result = super(LoginUserDetail, self)._check_credentials(password)
ip_address = request.httprequest.environ['REMOTE_ADDR']
vals = {'name': self.name,
'ip_address': ip_address
}
self.env['login.detail'].sudo().create(vals)
return result


Avatar
Discard
Author Best Answer

this is good solution.. but if i dont have internet it not work "as i think" there is no any code in openerp to get the IP without any third party?

Avatar
Discard
Related Posts Replies Views Activity
0
Mar 15
3682
0
Nov 17
3247
2
Mar 15
4400
1
Nov 24
39
2
Jul 22
13734