On Fri, Aug 25, 2017 at 8:33 PM, Steven D'Aprano <st...@pearwood.info> wrote:
> On Fri, Aug 25, 2017 at 07:13:12PM -0500, boB Stepp wrote:
>
>> My objective:  Determine who the currently logged in user is
>
> py> import os
> py> os.getlogin()
> 'steve'

Sweet!  Much better.  I did not scan the os modules closely enough.

>
>> and
>> determine if that user is in my list of users that I have authorized
>> to use my programs.
>
> That's probably best handled at the OS level, by setting your programs
> to be executable only by members of a particular group. In Linux, the
> relevant commands are chmod and chgrp but I don't know what they are in
> Solaris.

If that was all I needed the user name for (Sorry!  I did not mention
*everything*.), then I would probably go this route.  I also will have
certain default configuration information associated with each user.
But it sounds like it is probably better to do what you suggest, and
then do the configuration settings as planned.

>
>> I tried in the interpreter:
>>
>> =================================================================================================
>> py2> cmd = ['who', '-m']
>> py2> import subprocess as sp
>> py2> p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
>> py2> out, err = p.communicate()
>> py2> out
>> ''
>> py2> err
>> "Must be attached to terminal for 'am I' option\n"
>
> Check the documentation for who on your system, e.g. man who, but my
> guess is that the -m or "am I" option requires you to be attached to a
> terminal. By launching a subprocess, you're no longer attached to one.

I had checked the man pages for who, but did not appreciate/realize
that my launched subprocess was not attached to the user's terminal.
I probably should have realized this though, in retrospect.

> Under my Linux system, the above gives no output or error.
>
> No, I don't understand it either :-)
>
> But reading man who tells me that the -m option returns the user
> connected to stdin. Sure enough, this works:
>
> py> p = sp.Popen(cmd, stdin=sys.stdin, stdout=sp.PIPE, stderr=sp.PIPE)
> py> out, err = p.communicate()
> py> out
> 'steve    pts/5        2017-08-25 15:52 (:0.0)\n'

Works for me, too.  Thanks, Steve!

-- 
boB
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to