简体   繁体   English

Python Web套接字AttributeError:类型对象'_socketobject'没有属性'gethostbyname'

[英]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.

相关问题 Python属性错误:类型对象'_socketobject'没有属性'gethostbyname' - Python attribute error: type object '_socketobject' has no attribute 'gethostbyname' AttributeError:类型对象“ _socketobject”没有属性“ error” - AttributeError: type object '_socketobject' has no attribute 'error' AttributeError: '_socketobject' 对象没有属性 'error' - AttributeError: '_socketobject' object has no attribute 'error' AttributeError:'_socketobject'对象没有属性'bind' - AttributeError: '_socketobject' object has no attribute 'bind' 套接字编程,attributeerror'_socketobject'对象属性'bind'是只读的 - Socket programming, attributeerror '_socketobject' object attribute 'bind' is read-only AttributeError: 类型对象“socket”没有属性“socket” - AttributeError: type object 'socket' has no attribute 'socket' AttributeError:'_socketobject'对象没有属性'set_tlsext_host_name' - AttributeError: '_socketobject' object has no attribute 'set_tlsext_host_name' _socketobject'对象没有属性'bing' - _socketobject' object has no attribute 'bing' AttributeError: 'Response' 对象没有属性 'type' - Python Web Scraping Request - AttributeError: 'Response' object has no attribute 'type' - Python Web Scraping Request 修复 Python 中的“AttributeError: type object has no attribute” - Fix “AttributeError: type object has no attribute” in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM