Ir al contenido
Menú
Se marcó esta pregunta
7 Respuestas
12231 Vistas

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
Descartar

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.

Autor

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

Mejor respuesta

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
Descartar
Mejor respuesta

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
Descartar
Mejor respuesta

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
Descartar
Autor Mejor respuesta

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
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
0
mar 15
4495
0
nov 17
4033
2
mar 15
5333
1
nov 24
1067
2
jul 22
14970