Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Web套接字AttributeError:type object'_socketobject';没有属性';gethostbyname';_Python_Sockets_Websocket_Attributeerror - Fatal编程技术网

Python Web套接字AttributeError:type object'_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

我第一次尝试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 '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)