socket.sendto

socket.sendto(data, (host,port))

  • This function sends data from a socket
  • This refers to a client or server receiving data from the other
  • The data represents the data sent
  • The data needs to be a string
  • For example, socket.sendall('hello') will send 'hello' to a server socket
  • This function returns the number n of bytes sent to the server
  • The n may be lower than len(data) if these is no space in the server socket's buffet
  • The host and port refer to the detination host and port
  • It will block I/O until space appears

Example of sendto

>>> import socket
>>> sock = socket(AF_INET, SOCK_STREAM)

>>> # connect with server at ip
>>> # address 32.21.671.11 on port 25000
>>> address = ('32.21.671.11', 25000)
>>> sock.sendto('hello world', address)  # send to server

References

Previous
Next

socket.sendall

Echo Server