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

发布于 2024-12-22 18:31:19 字数 4560 浏览 0 评论 0原文

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

[email protected]:~
$ 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'
>>> 

[email protected]:~
$ hostname
matt-mmf-macbook.local

[email protected]:~
$ hostname -f
matt-mmf-macbook.local

socket.getfqdn() 的意外输出导致我的 Duplicity 备份失败,输出如下。我上次成功的口是心非备份是在 12 月 19 日。

[email protected]:~
$ ~/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

[email protected]:~
$ 

我的 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)/

Why does Python's socket.getfqdn() return '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' rather than matt-mmf-macbook.local?

[email protected]:~
$ 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'
>>> 

[email protected]:~
$ hostname
matt-mmf-macbook.local

[email protected]:~
$ hostname -f
matt-mmf-macbook.local

The unexpected output of socket.getfqdn() is making my Duplicity backups fail with the output below. My last successful Duplicity backup was on 12/19.

[email protected]:~
$ ~/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

[email protected]:~
$ 

My backup.sh contains:

#! /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)/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云朵有点甜 2024-12-29 18:31:19

我遇到了类似的问题。 getfqfn() 返回“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

导致返回正确的主机名。

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

I ran into a similar problem. getfqfn() was returning 'local.localdomain' instead of the value 'host.mydomain.com' returned by hostname. Checking the getfqfn() docs shows:

Return a fully qualified domain name for name. If name is omitted or
empty, it is interpreted as the local host. To find the fully
qualified name, the hostname returned by gethostbyaddr() is checked
,
followed by aliases for the host, if available. The first name which
includes a period is selected. In case no fully qualified domain name
is available, the hostname as returned by gethostname() is returned.

In my case, getfqdn() was actually looking in my /etc/hosts, which listed:

127.0.0.1 local.localdomain localhost host.mydomain.com

Since the "real" hostname was at the end, and "local.localdomain" is "the first name which includes a period", that's the one it used. Changing my /etc/hosts to:

127.0.0.1 host.mydomain.com local.localdomain localhost

caused the correct hostname to be returned.

I think a similar issue is occurring for you.

素年丶 2024-12-29 18:31:19

我假设它只是跳过本地 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,加上本地定义的名称 - 但互联网不知道这个名称。

I'd assume that it simply skips the local DNS resolution and goes straight to asking the internet for the hostname.

The only correct hostname for the IPv6 address ::1 is 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, plus the name defined locally - but the internet doesn't know that one.

仅此而已 2024-12-29 18:31:19

我导航到“系统偏好设置”>“网络>无线网络>高级> TCP/IP,并将“配置 IPv6”从: 更改

Off

为:

Automatically

现在我得到了我所期望的:

[email protected]:~
$ python -c 'import socket ; print socket.getfqdn()'
matt-mmf-macbook.local

[email protected]:~
$ 

我还注意到,在更改为“自动”后,“关闭”不再是一个选项。

I navigated to System Preferences > Network > Wi-Fi > Advanced > TCP/IP, and changed Configure IPv6 from:

Off

to:

Automatically

And now I get what I expect:

[email protected]:~
$ python -c 'import socket ; print socket.getfqdn()'
matt-mmf-macbook.local

[email protected]:~
$ 

I also notice that Off is no longer an option after changing to Automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文