简体   繁体   English

获取 127.0.1.1 而不是 192.168.1.* ip ubuntu python

[英]Getting 127.0.1.1 instead of 192.168.1.* ip ubuntu python

I am new to python.我是 python 的新手。 I want to get the ipaddress of the system.我想获取系统的IP地址。 I am connected in LAN.我连接在局域网中。 When i use the below code to get the ip, it shows 127.0.1.1 instead of 192.168.1.32.当我使用以下代码获取 ip 时,它显示 127.0.1.1 而不是 192.168.1.32。 Why it is not showing the LAN ip.为什么它没有显示 LAN ip。 Then how can i get my LAN ip.那我怎样才能得到我的 LAN ip。 Every tutorials shows this way only.每个教程都仅以这种方式显示。 I also checked via connecting with mobile hotspot.我还通过连接移动热点进行了检查。 Eventhough, it shows the same.尽管,它显示相同。

import socket    
hostname = socket.gethostname()    
IPAddr = socket.gethostbyname(hostname)    
print("Your Computer Name is:" + hostname)    
print("Your Computer IP Address is:" + IPAddr)    

Output: Output:

Your Computer Name is:smackcoders
Your Computer IP Address is:127.0.1.1

Required Output:所需 Output:

Your Computer Name is:smackcoders
Your Computer IP Address is:192.168.1.32

I got this same problem with my raspi.我的 raspi 也遇到了同样的问题。

host_name = socket.gethostname()`
host_addr = socket.gethostbyname(host_name)

and now if i print host_addr, it will print 127.0.1.1.现在,如果我打印 host_addr,它将打印 127.0.1.1。 So i foundthis: https://www.raspberrypi.org/forums/viewtopic.php?t=188615#p1187999所以我发现了这个: https ://www.raspberrypi.org/forums/viewtopic.php ? t = 188615#p1187999

host_addr = socket.gethostbyname(host_name + ".local")

and it worked.它奏效了。

As per the above '/etc/hosts' file content, you have an IP address mapping with '127.0.1.1' to your hostname.根据上面的“/etc/hosts”文件内容,您有一个 IP 地址映射,其中“127.0.1.1”到您的主机名。 This is causing the name resolution to get 127.0.1.1.这导致名称解析获得 127.0.1.1。 You can try removing/commenting this line and rerun.您可以尝试删除/注释此行并重新运行。

How can I get the IP address of eth0 in Python? 如何在 Python 中获取 eth0 的 IP 地址?

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
print s.getsockname()[0]

i get the same problem what your are facing.我遇到了你所面临的同样问题。 but I get the solution with help of my own idea, And don't worry it is simple to use.但是我在我自己的想法的帮助下得到了解决方案,而且不用担心它很容易使用。 if you familiar to linux you should heard the ifconfig command which return the informations about the network interfaces, and also you should understand about grep command which filter the lines which consist specified words now just open the terminal and type如果你熟悉 linux 你应该听过ifconfig命令,它返回有关网络接口的信息,你也应该了解grep命令,它过滤包含指定单词的行现在只需打开终端并键入

ifconfig | grep 255.255.255.0

and hit enter now you will get wlan inet address line alone like below然后按enter您将单独获得 wlan inet 地址行,如下所示

inet 192.168.43.248  netmask 255.255.255.0  broadcast 192.168.43.255

in your terminal in your python script just insert在你的python脚本的终端中插入

#!/usr/bin/env python
import subprocess
cmd = "ifconfig | grep 255.255.255.0"
inet = subprocess.check_output(cmd, shell = True)
inet = wlan.decode("utf-8")
inet = wlan.split(" ")
inet_addr = inet[inet.index("inet")+1]
print(inet_addr)

this script return your local ip address, this script works for me and I hope this will work for your linux machine all the best这个脚本返回你的本地 IP 地址,这个脚本对我有用,我希望这对你的 linux 机器最好

This also worked for me:这也对我有用:

gethostbyname(gethostname()+'.')

This solution works for me on Windows.这个解决方案适用于我在 Windows 上。 If you're using Linux you could try this line of code instead:如果您使用的是 Linux,则可以尝试使用以下代码行:

IPAddr = socket.gethostbyname(socket.getfqdn())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM