首页 > 解决方案 > Python Socket 函数错误,找不到 socket.gethostbyname

问题描述

我正在编写一个小程序来监控我的网站以及它们是否仍然在线。

我在 Visual Studio Code 中尝试在我的 Windows 计算机上进行以下操作,它起作用了:

import socket
hostName = "example.org"
ipAddress = socket.gethostbyname(hostName)
print(ipAddress)

但我想在 Ubuntu 系统上运行它,所以我将它转移到了 Ubuntu。但在这里我总是得到以下编译错误:

Traceback (most recent call last):
File "socket.py", line 1, in <module>
import socket

File "/root/Music/socket.py", line 3, in <module>
ipAddress = socket.gethostbyname(hostName)
AttributeError: module 'socket' has no attribute 'gethostbyname'

有什么我忘记了吗?对不起,我是新手,所以我会感谢每一个帮助。

我检查了,但套接字库是最新的:

pip3 install sockets
Requirement already satisfied: sockets in /usr/local/lib/python3.7/dist-packages (1.0.0)

我还卸载并安装了套接字。

标签: python-3.xsockets

解决方案


File "/root/Music/socket.py"似乎不是您想要的标准套接字模块,只是首先出现在模块搜索路径中。您应该删除文件socket.pysocket.pyc/root/Music.


推荐阅读