0
import socket
import sys

HOST = ''   # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

s.listen(10)
print 'Socket now listening'

#wait to accept a connection - blocking call
conn, addr = s.accept()

#display client information
print 'Connected with ' + addr[0] + ':' + str(addr[1])

While running this file I get

  File "socket.py", line 1, in <module>
    import socket
  File "/root/socket.py", line 7, in <module>
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AttributeError: 'module' object has no attribute 'AF_INET'

I want to create a socket to accept connections and read/send data... Im experiencing this kind of error while doing it. I am new to python i dont know anything i just got the code from a website. thankyou

1

2 Answers 2

4

You called your file socket.py rename it to somethng like my_socket.py and delete the socket.pyc file in the same directory.

You are trying to import from your socket.py file not the actual socket module, python checks the local directory first.

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

1 Comment

Quick explanation: When using the import statement, Python will first look in the current directory for a file that is named like that. So your file is imported instead of the library socket
0

The error simply means that the socket object does not have an error field or attribute

I think it is a naming issue you have since you names your file as the name of socket module. Change the name, might fix the problem.

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.