Search code examples
pythonmacossocketsipc

Find PID of the connected Unix Domain Socket


I've written simple client server program in python with file socket in Mac OS. I would like to know the process id of the connected client socket from server. I achieved in linux, but I couldn't find any way for Mac OS.


Solution

  • Based on this Golang issue implementing the same thing, you can do

    sock, addr = server.accept()
    other_pid = sock.getsockopt(0, 2)
    

    where 0 corresponds to SOL_LOCAL and 2 to LOCAL_PEERPID.

    I just tried it out – works fine on my Mac.