Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "TypeError: 'module' object is not callable" with `socket.socket()`

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.

like image 910
Arnav Garg Avatar asked Nov 18 '25 12:11

Arnav Garg


1 Answers

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

like image 99
argo Avatar answered Nov 21 '25 01:11

argo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!