socket.getpeername

socket.getpeername()

  • This function returns the remote address to which the socket is connected
  • Typically, this refers to the server address
  • The return value is usually a tuple (ip_address, port)
  • The client socket must be connected already

Example of getpeername

>>> import socket
>>> sock = socket(AF_INET, SOCK_STREAM)  # client socket
>>> serv_addr = ('localhost', 25000)
>>> sock.connect(serv_addr)
>>> sock.getpeername()
('127.0.0.1', 25000)

References

Previous
Next

socket.getsockname

socket.getsockopt