Python Web套接字AttributeError:type object'_socketobject';没有属性';gethostbyname';
我第一次尝试python web sockets,发现了以下错误: AttributeError:类型对象“\u socketobject”没有属性“gethostbyname”Python Web套接字AttributeError:type object'_socketobject';没有属性';gethostbyname';,python,sockets,websocket,attributeerror,Python,Sockets,Websocket,Attributeerror,我第一次尝试python web sockets,发现了以下错误: AttributeError:类型对象“\u socketobject”没有属性“gethostbyname” from socket import * serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket.bind((socket.gethostname(), 80)) serverSocket.listen(5) while True: print 'Re
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()
此外,如果您有任何建议供库使用,以使这更容易/更像您实际编写的内容,我将非常感谢您的输入 由于已将整个
socket
模块导入命名空间,因此在调用gethostname
函数之前,无需编写socket.
。将代码的第三行简化为:serverSocket.bind((gethostname(),80))
由于您将整个socket
模块导入了名称空间,因此在调用gethostname
函数之前不需要编写socket.
。将代码的第三行简化为:serverSocket.bind((gethostname(),80))
我应该提到的是,我使用的是Anaconda2.5.0(x86_64),我应该提到的是,我使用的是Anaconda2.5.0(x86_64)