Al Berger
Al Berger

Reputation: 1068

How to identify a client connected to AF_UNIX socket?

What is the meaning of the "client address" in case of AF_UNIX sockets in Python?

sock = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM )
sock.bind( "/tmp/sock" )
sock.listen( 1 )
while True:
    connection, client_address = sock.accept()
    print( client_address ) # prints : b''

Is there a way to obtain on the server side any information about the connected client?

Upvotes: 2

Views: 1128

Answers (1)

James Mills
James Mills

Reputation: 19050

For UNIX Sockets; socket.acept() will return socket, (). i.e: An empty tuple.

You can get some information about the "client" socket by looking at socket.fileno() for example.

For example with a modified echoserverunix.py:

$ python examples/echoserverunix.py 
<registered[*] (<Debugger/* 19377:MainThread (queued=0) [S]>, <EchoServer/server 19377:MainThread (queued=2) [R]> )>
<started[server] (<EchoServer/server 19377:MainThread (queued=1) [R]> )>
<registered[select] (<Select/select 19377:MainThread (queued=0) [S]>, <EchoServer/server 19377:MainThread (queued=2) [R]> )>
<ready[server] (<EchoServer/server 19377:MainThread (queued=1) [R]>, ('/tmp/test.sock', None) )>
<_read[server] (<socket._socketobject object at 0x7fa4c0b8a210> )>
<connect[server] (<socket._socketobject object at 0x7fa4c0b8a1a0> )>
<socket._socketobject object at 0x7fa4c0b8a1a0>
6
()

According to the accept() man page hwoever:

Return Value

On success, these system calls return a nonnegative integer that is a descriptor for the accepted socket. On error, -1 is returned, and errno is set appropriately.

So at the C level you get back the "file descriptor" which Python creates a socket object out of. But there is no peer address or path that th client connected from other than the file descriptor.

Upvotes: 2

Related Questions