Joshua Talbot Joshua Talbot - 29 days ago 8
Python Question

'int' object is not callable (python 2.7 server/client calculator)

Okay, first off I know this question has been asked quite a few times, however, none of the previous questions have included the use of UDP sockets.
I've tested this code without using UDP sockets and it works just fine.
Thank you in advance, here's my code:

Client.py:

from socket import *
serverName = "localhost"
serverPort = 12000
clientSocket = socket(AF_INET , SOCK_DGRAM)
num1 = int(input("1st number: \n"))
func = raw_input("Function (+,-,*,/): \n")
num2 = int(input("2nd number: \n"))
clientSocket.sendto(int(num1), func, int(num2)(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print (modifiedMessage)
clientSocket.close()


Server.py:

from socket import *
serverPort = 12000
serverSocket = socket ( AF_INET , SOCK_DGRAM )
serverSocket.bind (( '', serverPort ))
print ('The Server is ready ')
while 1:
num1, func, num2, clientAddress = serverSocket.recvfrom(2048)
if func =='+':
modifiedMessage = num1 + num2
else:
pass
if func =='-':
modifiedMessage = num1 - num2
else:
pass
if func =='*':
modifiedMessage = num1 + num2
else:
pass
if func =='/':
modifiedMessage = num1 / num2
else:
pass
serverSocket.sendto(modifiedMessage, clientAddress)


Traceback (most recent call last):
File"C:\Users\Myhat2you\Desktop\client.p", line 8, in clientSocket.sendto(int(num1), func, int(num2)(serverName, serverPort)) TypeError: 'int' object is not callable

Answer

Problem is indeed here:

clientSocket.sendto(int(num1), func, int(num2)(serverName, serverPort))

According to docs, sendto accepts str, not int, str and int.

However, traceback You got is there because You tried to call some int value as function (say num2 is "5", then, int(num2) will be 5, and You're calling 5(serverName, serverPort).

If You want to send those values to server, I suggest one of the following:

1) send them one by one

2) send them as string separated by some special character (ie. |)

Comments