【问题标题】:Python Web Socket AttributeError: type object '_socketobject' has no attribute 'gethostbyname'Python Web Socket AttributeError:类型对象“_socketobject”没有属性“gethostbyname”
【发布时间】:2016-03-01 15:30:39
【问题描述】:

第一次尝试使用 python web sockets 并且遇到了这个错误:

AttributeError: type object '_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()

另外,如果您对库有任何建议可以使这更容易/更像您实际编写的内容,我将非常感谢您的意见。

【问题讨论】:

  • 我应该提一下,我使用的是 Anaconda 2.5.0 (x86_64)

标签: python sockets websocket attributeerror


【解决方案1】:

由于您将整个 socket 模块导入命名空间,因此您无需在调用 gethostname 函数之前编写 socket.。像这样简化代码的第三行:serverSocket.bind((gethostname(), 80))

【讨论】:

    猜你喜欢
    • 2010-12-23
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多