3
import socket
from socket import *

conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))

I'm facing this error:

AttributeError: type object 'socket' has no attribute 'socket'

5 Answers 5

9

If you import all library don't need to name library before method:

from socket import *
socket.socket # AttributeError: type object 'socket' has no attribute 'socket'
from socket import *
socket # <class 'socket.socket'>

or maybe :

import socket
socket.socket # <class 'socket.socket'>
Sign up to request clarification or add additional context in comments.

Comments

2
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print("Socket is created")

I fixed these changes my codes and work.

Comments

0

You've imported socket and then imported * from socket.

As socket has a socket function in it python is likely getting confused and using the socket inside socket when you're calling socket.socket.

I would drop the from socket import * and work from there.

3 Comments

Corrected by then i get a different bug again: conn.bind((socket.gethostname(), 80)) OSError: [Errno 19] No such device. Any help with that please
What do you get then? If it's similar then the modules it's complaining about try importing separately. for example from socket import thismodule
If you're running linux then only root can run something on port 80. To test, try running it on 8080. Other than that it might be that something else is running on 80 like apache or nginx
0

socket(AF_INET, SOCK_DGRAM)

python is a little simpler

Comments

0

"If you import all library don't need to name library before method:"

Option 1 - import library:

import socket

conn = socket.socket( socket.AF_INET, socket.SOCK_STREAM )

Option 2 - from library import all:

from socket import *

conn = socket( AF_INET, SOCK_STREAM )

2 Comments

This solution is already covered by this answer. When answering old questions, please ensure that your answer provides a distinct and valuable contribution to the Q&A.
@snakecharmerb My apologies. I found that answer helpful, but I needed concrete examples to fully understand how to solve the problem. I wanted to provide such examples and state them in a clearer way.

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.