Python attribute error: type object '_socketobject' has no attribute 'gethostbyname'

那年仲夏 提交于 2019-12-04 11:05:38

问题


I am trying to do this in my program:

dest = socket.gethostbyname(host)

I have included the line:

from socket import * 

in the beginning of the file.

I am getting this error:

AttributeError: type object '_socketobject' has no attribute 'gethostbyname'

I am running Vista 64bit. Could there be a problem with my OS? I have turned down my firewall and everything.


回答1:


You shoulod either use

import socket
dest = socket.gethostbyname(host)

or use

from socket import *
dest = gethostbyname(host)

Note: the first option is by far the recommended one.




回答2:


After from socket import *, you'd need to call just the barename gethostbyname -- the barename socket now refers to a type, not to the module. That import * is horrible practice, by the way: do, instead, import socket, and then socket.gethostbyname will work just fine!



来源:https://stackoverflow.com/questions/1857146/python-attribute-error-type-object-socketobject-has-no-attribute-gethostbyn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!