为什么Python的socket.getfqdn()返回一个看起来像IPv6主机的长字符串,而不是`hostname -f`返回的结果?

5
为什么Python的socket.getfqdn()返回"1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa"而不是matt-mmf-macbook.local?
mlm@matt-mmf-macbook.local:~
$ python
Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) 
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.getfqdn()
'1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa'
>>> 

mlm@matt-mmf-macbook.local:~
$ hostname
matt-mmf-macbook.local

mlm@matt-mmf-macbook.local:~
$ hostname -f
matt-mmf-macbook.local
< p > socket.getfqdn() 的意外输出导致我的 Duplicity 备份失败,以下是输出。我的上一次成功的 Duplicity 备份是在 12/19。

mlm@matt-mmf-macbook.local:~
$ ~/config/bash/backup.sh
Reading globbing filelist /Users/mlm/config/bash/backup-include-matt-mmf-macbook.txt
Reading globbing filelist /Users/mlm/config/bash/backup-exclude-matt-mmf-macbook.txt
Local and Remote metadata are synchronized, no sync needed.
Warning, found the following remote orphaned signature files:
duplicity-new-signatures.20110929T140604Z.to.20110929T143209Z.sigtar.gpg
duplicity-new-signatures.20110929T143209Z.to.20110929T150055Z.sigtar.gpg
duplicity-new-signatures.20110929T150055Z.to.20110929T161503Z.sigtar.gpg
duplicity-new-signatures.20110929T161503Z.to.20110930T161505Z.sigtar.gpg
duplicity-new-signatures.20110930T161505Z.to.20111005T024235Z.sigtar.gpg
duplicity-new-signatures.20111005T024235Z.to.20111005T024907Z.sigtar.gpg
duplicity-new-signatures.20111005T024907Z.to.20111005T161508Z.sigtar.gpg
duplicity-new-signatures.20111005T161508Z.to.20111006T161509Z.sigtar.gpg
duplicity-new-signatures.20111006T161509Z.to.20111007T161507Z.sigtar.gpg
duplicity-new-signatures.20111007T161507Z.to.20111010T161511Z.sigtar.gpg
duplicity-new-signatures.20111010T161511Z.to.20111011T161507Z.sigtar.gpg
duplicity-new-signatures.20111011T161507Z.to.20111012T161510Z.sigtar.gpg
duplicity-new-signatures.20111012T161510Z.to.20111013T161505Z.sigtar.gpg
duplicity-new-signatures.20111013T161505Z.to.20111017T161506Z.sigtar.gpg
duplicity-new-signatures.20111017T161506Z.to.20111018T161505Z.sigtar.gpg
duplicity-new-signatures.20111018T161505Z.to.20111019T161506Z.sigtar.gpg
duplicity-new-signatures.20111019T161506Z.to.20111020T161506Z.sigtar.gpg
duplicity-new-signatures.20111020T161506Z.to.20111021T161511Z.sigtar.gpg
duplicity-new-signatures.20111021T161511Z.to.20111025T161507Z.sigtar.gpg
duplicity-new-signatures.20111025T161507Z.to.20111026T161510Z.sigtar.gpg
duplicity-new-signatures.20111026T161510Z.to.20111027T161506Z.sigtar.gpg
duplicity-new-signatures.20111027T161506Z.to.20111028T161511Z.sigtar.gpg
duplicity-new-signatures.20111028T161511Z.to.20111104T161506Z.sigtar.gpg
duplicity-new-signatures.20111104T161506Z.to.20111115T222417Z.sigtar.gpg
Last full backup date: Wed Nov 16 12:16:14 2011
Fatal Error: Backup source host has changed.
Current hostname: 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa
Previous hostname: matt-mmf-macbook.local

Aborting because you may have accidentally tried to backup two different data sets to the same remote location, or using the same archive directory.  If this is not a mistake, use the --allow-source-mismatch switch to avoid seeing this message

mlm@matt-mmf-macbook.local:~
$ 

我的backup.sh文件包含以下内容:

#! /bin/bash

PATH=/usr/local/bin:/Library/Frameworks/Python.framework/Versions/2.6/bin:$PATH

. ~/config/bash/awskeys.sh

. $(type -p virtualenvwrapper.sh)
workon duplicity

ulimit -n 1024

duplicity \
    --encrypt-key DEADBEEF \
    --include-globbing-filelist ~/config/bash/backup-include-$(hostname -s).txt \
    --exclude-globbing-filelist ~/config/bash/backup-exclude-$(hostname -s).txt \
    / \
    s3://s3.amazonaws.com/backup-$(hostname -s)/
3个回答

3
我遇到了类似的问题。getfqdn()返回的是'local.localdomain'而不是由hostname返回的值'host.mydomain.com'。查看getfqfn()文档显示:
返回name的完全限定域名。如果省略或为空,则将其解释为本地主机。要找到完全限定名称,首先检查由gethostbyaddr()返回的主机名,然后检查主机的别名(如果有)。选择包含句点的第一个名称。如果没有可用的完全限定域名,则返回由gethostname()返回的主机名。
在我的情况下,getfqdn()实际上在我 /etc/hosts中查询:
127.0.0.1 local.localdomain localhost host.mydomain.com

由于“真实”的主机名位于末尾,而“local.localdomain”是“第一个包含句点的名称”,因此它使用了该名称。将我的/etc/hosts更改为:

127.0.0.1 host.mydomain.com local.localdomain localhost

导致正确的主机名被返回。

我认为你也遇到了类似的问题。


谢谢,Python 在这方面真的很愚蠢 - 它应该只告诉我们“hostname -f”所说的内容,而不要想太多。 - Peter K
不要像建议的那样更改hosts文件的第一行,而是应在前两个“标准”条目后添加一行,其中包含“ipAddress host.mydomain.com host”。这也应该可以解决问题,而不会更改一般情况下不应更改的localhost设置。 - Neil H

1

我认为它只是跳过本地DNS解析,直接向互联网询问主机名。

IPv6地址::1的唯一正确主机名是1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa,再加上在本地定义的名称 - 但互联网不知道那个名称。


似乎它正在解析 ::1 而不是 127.0.0.1。你知道为什么在我 MacBook Pro 在 12/19 和 12/23 之间会发生这样的变化吗? - Matt McClure
OS X 使用算法在 IPv4 和 IPv6 之间进行选择。 应用程序应该同时支持v4和v6,或者在应用程序不支持v6时简单地回退到v4。 “为什么OS X要这样做?” 不应该是相关的。 - Tom van der Woerdt
Tom,感谢你的帮助。我找到了问题并在这个问题下添加了一个答案。 - Matt McClure

0

我打开了“系统偏好设置”>“网络”>“Wi-Fi”>“高级”>“TCP/IP”,并将“配置IPv6”从原来的设置改为:

Off

至:

Automatically

现在我得到了我期望的结果:

mlm@matt-mmf-macbook.local:~
$ python -c 'import socket ; print socket.getfqdn()'
matt-mmf-macbook.local

mlm@matt-mmf-macbook.local:~
$ 

我也注意到在更改为“自动”后,“关闭”选项不再可用。


所以,你关闭IPv6而不是修复你的代码?我不确定这是否是一个面向未来的解决方案 :-) 实际上,这意味着我永远无法运行你的软件。 - Tom van der Woerdt
我觉得你误解了。IPv6之前是“关闭”的;我不知道为什么或者它是如何变成那样的。我将IPv6更改为“自动”,这解决了我原来问题中的问题。 - Matt McClure

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接