Python套接字函数错误,找不到socket.gethostbyname

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

我尝试在Windows计算机上使用Visual Studio Code进行以下操作,并且该方法有效:

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)

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

hl994 回答:Python套接字函数错误,找不到socket.gethostbyname

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

本文链接:https://www.f2er.com/3166058.html

大家都在问