Know my private ip from python


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 ( 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(("", 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

2 answers


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 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(("", 80))
answered by 29.07.2018 / 01:58

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