本文實例講述了python實現(xiàn)得到當前登錄用戶信息的方法。分享給大家供大家參考,具體如下:
在linux 環(huán)境下,python 更多的被當做 替代 SHELL 的工具語言, 其實linux 中,本身就有很多命令是通過python擴展的,我想記錄下一些常用的命令以及使用方式,以便以后查看.
第一部分:python得到得到當前登錄用戶信息
def get_current_user(): try: # pwd is unix only import pwd return pwd.getpwuid(os.getuid())[0] except ImportError, e: import getpass return getpass.getuser() def get_default_group_for_user(user): import grp group = None try: gid= pwd.getpwnam(user)[3] groups = grp.getgrgid(gid)[0] except KeyError, e: print( 'Failed to find primary group from user %s' ,user) return group
注意的是 pwd, grp 模塊只能在linux,unix 下才有的. 我在網(wǎng)上搜索到了另一個在window 下得到用戶組相關信息的方法,但需要安裝? Python Win32 Extensions 模塊。可以在這里下載 (http://starship.python.net/crew/mhammond/win32/),使用方法如下:
import win32net import platform import getpass #Get current hostname and username sHostname = platform.uname()[1] sUsername = getpass.getuser() #Define account memberships to test as false memberAdmin = False memberORA_DBA = False for groups in win32net.NetUserGetLocalGroups(sHostname,sUsername): #If membership present, set to true if groups == 'Administrators': print "member of admin" memberAdmin = True if groups == 'ORA_DBA': print "member of orA_DBA" memberORA_DBA = True #if all true pass, else fail if (memberAdmin == True) and (memberORA_DBA == True): print "membership is good" else: print "current account does not have the proper group membership"
得到用戶名,當然只是第一步,后面還涉及到修改權限等操作。后面有時間再記錄.
希望本文所述對大家基于Django框架的Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
