1

I am building a simple UDP client, however, I am getting an error message "socket is not callable". I have checked the code and it can find the problem

I tried importing the module by using "from socket import *" but it didn't work.

import socket
serverName = 'hostname'
serverPort = 1200
clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)
message = 'Testing the system'
clientSocket.sendto(message, (serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print(modifiedMessage)
clientSocket.close()
Osborne Saka
  • 469
  • 1
  • 4
  • 21

3 Answers3

1

you need to import socket from socket module like this

import socket
from socket import socket as sk
serverName = 'hostname'
serverPort = 1200
clientSocket = sk(socket.AF_INET, socket.SOCK_DGRAM)

etc

0

I think you have named your program as socket.py or stored your program in a folder that contains socket.py . Either rename your file or delete them

krishna reddy
  • 295
  • 2
  • 15
0

Your code should work, you just need to change

clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)

To

clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

You are trying to feed inputs to a module (socket) instead of the function inside the module (socket.socket)

Reedinationer
  • 5,661
  • 1
  • 12
  • 33