python-3.x - 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)
我还卸载并安装了套接字。
解决方案
File "/root/Music/socket.py"
似乎不是您想要的标准套接字模块,只是首先出现在模块搜索路径中。您应该删除文件socket.py
并socket.pyc
从/root/Music
.
推荐阅读
- java - PreparedStatement IN子句Regexp替代?
- javascript - 在哈希数组中减去值 - Javascript
- python - 将响应类型存储到数据框
- python - 在python中添加特定索引位置的内容
- android-studio - 立即隐藏密码文本 Android Studio
- javascript - 试图从嵌套数组中的 json 对象中获取值
- python-3.x - 如何创建可调用对象来模拟 API 调用?
- java - 如何在java之外向kryonet发送数据
- database - 为什么 elasticsearch 不返回包含几乎相同单词的文档?
- javascript - 如果字符串为空并且仍然引发错误,则 .notRequired 对 yup 字符串不起作用