Know my private ip from python

2

I am creating a small chat that will work in a local network, I explain the situation: where I study there are 3 rooms with computers and pages like facebook, twitter and other social networks are blocked, so I thought it would be a good idea to create a chat for the rest of the students (to earn some money), for this I thought about put servers on 2 or 3 computers (if any is off), I'm too lazy to go to%% and edit ipconfig (127.0.0.1 is just to put an ip) in each of the computers, in addition to fiddling a bit I realized that the router restarted (because when I put sock.bind(("127.0.0.1", 5000)) for some reason or another on the same computer on a different day I realize that the ip changes), so I was wondering if there is any function of a library or something that returns the private ip of the machine that is being used?

    
asked by binario_newbie 27.07.2018 в 09:12
source

2 answers

2

There are several ways to find the IP address of your computer in python . One option is to make a connection to Google's DNS (available on the Internet at address 8.8.8.8) and review the address used as the source of the communication. Using this option, you can detect the IP address that has Internet access.

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
print(s.getsockname()[0])
s.close()
    
answered by 29.07.2018 / 01:58
source
0

I suggest you configure your ip to be static, clear with the ip, subnet mask and default gateway, with which it is active. I hope it helps you. Greetings

    
answered by 29.07.2018 в 01:51