2
votes

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
you are missing from socket import socket. Duplicate of stackoverflow.com/a/4534443/2375207 - nicolallias

1 Answers

6
votes

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