3

When I execute this code I get the following error:

import socket
s=socket.socket()
s.connect(("data.pr4e.org",80))
cmd='GET http://data.pr4e.org/romeo.txt HTTP/1.0\n\n'.encode()
s.send(cmd)
while True:
    data=s.recv(512)
    if (len(data)<1):
            break
    print(data.decode())
s.close()

Error:

Traceback (most recent call last):
  File "socket.py", line 1, in <module>
    import socket
  File "/home/arnav/WorkSpace/Python/Coursera/AccessWebData/socket.py", line 2, in <module>
    s=socket.socket()
TypeError: 'module' object is not callable

When this code is entered on the terminal, it works just fine.

I do not understand what I am doing wrong.

1
  • 1
    @nicolallias He is not missing from socket import socket since he is doing s=socket.socket() instead of s=socket(). Also i wouldnt consider this a duplicate since he added much more context than the original question. Commented Jan 6, 2023 at 15:06

1 Answer 1

10

The main issue is your file name is also socket.py and the first line of your code is also import socket. So basically python code is trying to import itself and hence it is failing.

Please rename the file to socket_use_case.py and it would solve your problem

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.