[英]Python Web Socket AttributeError: type object '_socketobject' has no attribute 'gethostbyname'
Trying my hand at python web sockets for the first time and stuck on this bug: 第一次尝试使用python Web套接字,并停留在此bug上:
AttributeError: type object '_socketobject' has no attribute 'gethostbyname' AttributeError:类型对象“ _socketobject”没有属性“ gethostbyname”
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind((socket.gethostname(), 80))
serverSocket.listen(5)
while True:
print 'Ready to serve.'
connectionSocket, addr = serverSocket.accept()
print 'connection is from', addr
try:
message = connectionSocket.recv(2048)
filename = message.split()[1]
print filename
f = open(filename[1:])
outputdata = f.read()
connectionSocket.send('HTTP/1.1 200 OK\r\n\r\n')
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i])
connectionSocket.close()
except IOError:
print 'IOError'
connectionSocket.send('HelloWorld.html')
connectionSocket.close()
serverSocket.close()
Also, if you have any recommendations for libraries to use to make this easier/more like something you'd actually write, I'd much appreciate the input. 另外,如果您对库有什么建议,可以使此操作更容易/更像您实际编写的内容,我将非常感谢您的输入。
Since you imported the whole socket
module into your namespace, you do not need to write socket.
由于您将整个
socket
模块导入了您的名称空间,因此您无需编写socket.
before your call to the gethostname
function. 在调用
gethostname
函数之前。 Simplify the third line of your code like so: serverSocket.bind((gethostname(), 80))
像这样简化代码的第三行:
serverSocket.bind((gethostname(), 80))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.