python / python/cpython

socket.getfqdn() returns unqualifed hostname if the resolver is configured to trim local domains

未關閉
#100,298 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib topic-socket type-bug
主要語言
Python
星號
77.2k
分支
36k
平均合併
1 天 9 小時
30 天內合併 PR
558

描述

Bug report

socket.getfqdn() is returning an unqualified address for my host. I tracked this down to the use of the trim keyword in /etc/host.conf the removal of which would cause other unrelated problems.

Looking at the source code for the system hostname command, they appear to call getaddrinfo with the service parameter set to NULL and pull out the ai_canonname field from the result. See: https://sources.debian.org/src/hostname/3.23/hostname.c lines 332-340 and 386 The python getaddrinfo doesn't especially help with this but perhaps this provides clues as to a more robust implementation.

Looking at Lib/socket.py, gethostname() actually is returning a fully qualified hostname and the gethostbyaddr() step is turning it back into an unqualified address. If this is hard to do portably, perhaps it could check for a dot in the values.

Your environment

Tested on a variety of RHEL based systems - Oracle Linux and CentOS Stream versions 8 and 9 both with distributed Python(s) and a build from source of the latest Python. Can also verify the issue by breaking down and tracing the source code in cpython/Lib/socket.py

/etc/host.conf on these systems includes the trim directive to remove the local domain from returned results.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 Lib/socket.py 開始,追蹤 getfqdn() 經由 gethostname() 和 gethostbyaddr() 的呼叫流程;將其行為與 hostname 命令對 getaddrinfo 和 ai_canonname 的使用進行比較。在 /etc/host.conf 中設定 trim 的情況下重現該案例,並在 getfqdn() 無需移除該指示詞即可保留完整限定主機名稱時,視為結果完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
networking
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。