Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

OpenERP get User IP Address

By
Mohammad M Omar
on 2/11/15, 8:04 AM 1,239 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

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.

Yenthe
on 2/11/15, 8:14 AM

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

Mohammad M Omar
on 2/11/15, 8:21 AM
1

Vasanth

--Vasanth--
3458
| 5 3 8
Pondicherry, India
--Vasanth--
Vasanth
On 2/11/15, 8:37 AM

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()

1
Jordan Vrtanoski
On 2/11/15, 9:07 AM

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.

0
Mohammad M Omar
On 2/11/15, 8:47 AM

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?

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/11/15, 8:04 AM
Seen: 1239 times
Last updated: 3/16/15, 8:10 AM