为什么Python的socket.getfqdn()返回的长字符串像IPv6主机,而不是hostname -f返回的?

5 投票
3 回答
2589 浏览
提问于 2025-04-17 08:57

为什么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.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

这个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 个回答

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.ip6.arpa,还有一个是本地定义的名字——但互联网并不知道那个名字。

3

我遇到了类似的问题。getfqfn() 返回的是 'local.localdomain',而不是通过 hostname 得到的 'host.mydomain.com'。查看一下 getfqfn() 的文档,里面提到:

这个函数会返回一个完整的域名。如果没有提供名字或者名字是空的,它就会把它当作本地主机来处理。为了找到完整的名字,它会检查通过 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

就能返回正确的主机名了。

我觉得你遇到的情况也类似。

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:~
$ 

我还注意到,改成自动之后,关闭这个选项就不再可选了。

撰写回答