追溯(最近一次呼叫最近):AttributeError中的文件“",第1行:模块"socket"没有属性"close" [英] Traceback (most recent call last): File "", line 1, in AttributeError: module 'socket' has no attribute 'close'

查看:79
本文介绍了追溯(最近一次呼叫最近):AttributeError中的文件“",第1行:模块"socket"没有属性"close"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序来检索域名列表的IP地址.简单的示例可以在此处显示:

I need to write a program that retrieves the IP addresses of a list of domain names. The simple example can be shown here:

>>> import socket
>>> socket.gethostbyname('google.com')
'172.217.160.14'
>>> socket.close()

尝试关闭套接字后,出现此错误:

After I try to close the socket, I get this error:

回溯(最近一次通话最近):文件",第1行,在 AttributeError:模块套接字"没有属性关闭"

Traceback (most recent call last): File "", line 1, in AttributeError: module 'socket' has no attribute 'close'

如何关闭此插座?我需要关闭它,因为我的实际程序有循环,在列表中的每个域名中我都需要获取其IP,因此我需要在每次迭代中为新主机关闭套接字.

How can I close this socket? I need to close it because my actual program has loop where in each domain name in the list I needs to get its IP, so I need to close the socket in each iteration for the new host.

您能告诉我是什么问题吗?

Can you tell me what is the problem?

推荐答案

代码中的socket是指套接字模块,它没有close功能. Python 3.7中添加了新的模块级别关闭函数,但是它需要套接字描述符作为参数.如果我做对了,您想调用 socket对象的close方法,而不是模块.就您而言,您无需关闭任何连接.

The socket in your code refers to the socket module, which has no close function. A new module level close function has been added in Python 3.7, but it requires a socket descriptor as an argument. If I get it right, you want to call the close method of socket object, not the module. In your case you don't need to close any connection.

这篇关于追溯(最近一次呼叫最近):AttributeError中的文件“",第1行:模块"socket"没有属性"close"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆