tags:

views:

32

answers:

3

If Python, if you are developing a system service that communicates with user applications through sockets, and you want to treat sockets connected by different users differently, how would you go about that?

If I know that all connecting sockets will be from localhost, is there a way to lookup through the OS (either on windows or linux) which user is making the connection request?

+3  A: 

On Linux and other unixy system, you can use the ident service.

I'm not sure if Windows offers something similar.

Daniel Stutzbach
+1, totally forgot about `ident`.
Nikolai N Fetissov
A: 

On Linux you can get the source (i.e. client-side) port of the socket and parse the output of the lsof(8) utility searching for who is using that port.

Here's the manual page.

Nikolai N Fetissov
A: 

Unfortunately, at this point in time the python libraries don't support the usual SCM_CREDENTIALS method of passing credentials along a Unix socket.

You'll need to use an "ugly" method as described in another answer to find it.

MikeyB