1

I've problem with python socket at s.sendto(data,addr) and my code like this

import socket

    def Main():
        host = '127.0.0.1'
        port = 5000
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind((host, port))
        print("server started")
        while True:
            data, addr = s.recvfrom(1024)

            print ("message from : "+ str(addr))
            print ("from connected user : "+ str(data))
            data = str(data.upper())
            print ("sending : "+ str(data))
            socket.sendto(data, addr)

and result

    socket.sendto(data, addr)
AttributeError: module 'socket' has no attribute 'sendto'
        s.close()
    if __name__ == '__main__':
        Main()

and at UdpClient s.sendto is working

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

It seems that you mistyped the socket.sendto(... statement: The AttributeError is raised since the method sendto() is to be called from instances of the class socket.socket (as you have in s), and not from the socketmodule itself. See here for more details about the meaning of that statement.

So you basically need to change socket.sendto(... to s.sendto(...

also, if you want to check the attributes of any x object,(apart from reading the docs) you can simply check its x.__dict__ field, as explained here

cheers

Community
  • 1
  • 1
fr_andres
  • 5,927
  • 2
  • 31
  • 44