1

Trying my hand at python web sockets for the first time and stuck on this bug:

AttributeError: type object '_socketobject' has no attribute '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.

1
  • I should mention, I'm using Anaconda 2.5.0 (x86_64) Commented Mar 1, 2016 at 15:32

1 Answer 1

1

Since you imported the whole socket module into your namespace, you do not need to write socket. before your call to the gethostname function. Simplify the third line of your code like so: serverSocket.bind((gethostname(), 80))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.