getpass模块 ---- optparse

本文介绍了Python中的getpass模块及其使用方法,该模块可以在用户输入密码时不进行回显,增强安全性。此外还介绍了optparse模块,用于解析命令行参数。

getpass模块,不回显

常用方法

import getpass

password=getpass.getpass('input your password: ').strip()  #用户输入密码时,其他人看不到

print(password)

其他用法

user=getpass.getuser() #返回用户的登录名,使用于 unix,windows,这行命令在交互式界面不用你输入

注意:

#getpass模块在pycharm中无法使用,只能在其他交互式的界面使用,在cmd中验证可以

optparse模块

optparse是python用来解析命令行参数的

待补。。。。。

posted on 2018-08-09 17:19 dawn-liu 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/mmyy-blog/p/9450293.html

import os import re import random import getpass import socket import time import sys import stat import signal import threading import shutil import resource import math import subprocess from optparse import OptionParser # TODO set some global control parameter glbUserName = "" glbWorkPath = "" glbBlockPath = "" glbBlockName = "" compileStatus = 0 reapedCount = 0 curLiveCount = 0 reapedFlag = False bsubBusy = 0 # TODO Set enviroment color frontGroundColor = { "ANSI_BLACK" : 30, "ANSI_RED" : 31, "ANSI_GREED" : 32, "ANSI_YELLOW": 33, "ANSI_BLUE" : 34, "ANSI_PURPLE": 35, "ANSI_CYAN" : 36, "ANSI_WHITE" : 37 } # TODO define every print message by every color class setEnvMsg(): def __init__(self,message): self.msgContext = message self.fileError = "err.log" def writeMessage(self,message): self.msgContext = message def blackPrint(self,message): print('\033[1;30m' + message + '\033[0m') def redPrint(self,message): print('\033[1;31m' + message + '\033[0m') def greedPrint(self,message): print('\033[1;32m' + message + '\033[0m') def yellowPrint(self,message): print('\033[1;33m' + message + '\033[0m') def bluePrint(self,message): print('\033[1;34m' + message + '\033[0m') def purplePrint(self,message): print('\033[1;35m' + message + '\033[0m') def cyanPrint(self,message): print('\033[1;36m' + message + '\033[0m') def msgPrint(self,message,fg=frontGroundColor["ANSI_BLACK"]): if fg == frontGroundColor["ANSI_BLACK"]: self.blackPrint(message) elif fg == frontGroundColor["ANSI_RED"]: self.redPrint(message) elif fg == frontGroundColor["ANSI_GREED"]: self.greedPrint(message) elif fg == frontGroundColor["ANSI_YELLOW"]: self.yellowPrint(message) elif fg == frontGroundColor["ANSI_BLUE"]: self.bluePrint(message) elif fg == frontGroundColor["ANSI_PURPLE"]: self.purplePrint(message) elif fg == frontGroundColor["ANSI_CYAN"]: self.cyanPrint(message) def putBannerPrint(self,fgh=frontGroundColor["ANSI_RED"],fgb=frontGroundColor["ANSI_RED"]): self.msgPrint("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-",fgh) self.msgPrint("->\t" + self.msgContext,fgb) self.msgPrint("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-",fgh) def fatalMsgPrint(self,message): self.msgPrint("(FATAL:) " + message, frontGroundColor["ANSI_RED"]) sys.exit(1) def errorMsgPrint(self,message): self.msgPrint("(ERROR:) " + message, frontGroundColor["ANSI_YELLOW"]) def warnMsgPrint(self,message): self.msgPrint("(WARNING:) " + message, frontGroundColor["ANSI_BLUE"]) def infoMsgPrint(self,message): self.msgPrint("(INFO:) " + message, frontGroundColor["ANSI_BLACK"]) def debugMsgPrint(self,message): self.msgPrint("(DEBUG:) " + message, frontGroundColor["ANSI_BLACK"]) def errorMsgWrite(self,fileHandle,message): if fileHandle: fileHandle.write(message) def closeErrorFile(self,fileHandle): if fileHandle: fileHandle.close() # TODO Scripts to submit task to openlava class bsubTaskProcess(): def __init__(self): self.writeBusy = 0 self.readBusy = 0 self.msgCtrlObj = setEnvMsg("Bsub to manage task submit and post process") def excuteBsubCmd(self,command,bsubJobList,processPath): global bsubBusy while True: if bsubBusy==0: break; time.sleep(1) bsubBusy = 1 jobInfo = "Job <(\d+)> is submitted" if os.path.exists(processPath)==True: os.chdir(processPath) else: self.msgCtrlObj.fatalMsgPrint("No found the path: %0s"%(processPath)) p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,universal_newlines=True) time.sleep(1) while True: p.poll() line=p.stdout.readline() jobObj = re.search(jobInfo,line) if jobObj: jobId = jobObj.group(1) while self.readBusy==1: time.sleep(1) while self.writeBusy==1: time.sleep(1) if self.writeBusy==0: break self.writeBusy = 1 time.sleep(1) bsubJobList.append(jobId) self.writeBusy = 0 break else: jobId = None break p.wait() if p.returncode!=0: self.msgCtrlObj.fatalMsgPrint("Error occurred(%s) from "%(p.returncode) + command) bsubBusy = 0 return jobId def checkBsubJob(self,jobId): bsubJobStatus = 0 jobIdQueue = [] jobInfoList = [] if jobId==None: return p = subprocess.Popen(options.bJobNameCtrl+" -noheader", shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,universal_newlines=True) context=p.stdout.read() if "No unfinished job" not in context: for line in context.splitlines(): jobInfoList = line.split() jobIdQueue.append(jobInfoList[0]) if jobId in jobIdQueue: bsubJobStatus = 1 return bsubJobStatus def waitBsubCmdDone(self,jobId,bsubJobList): jobDoneCount = 3 if jobId==None: return time.sleep(10) while True: jobDoneStatus = 0 jobDoneIdx = 0 jobIdQueue = [] jobInfoList = [] time.sleep(5) p = subprocess.Popen(options.bJobNameCtrl+" -noheader", shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,universal_newlines=True) context=p.stdout.read() if "No unfinished job" not in context: for line in context.splitlines(): jobInfoList = line.split() jobIdQueue.append(jobInfoList[0]) else: return if jobId not in jobIdQueue: while True: time.sleep(1) jobDoneStatus = self.checkBsubJob(jobId) jobDoneIdx += 1 if jobDoneIdx==jobDoneCount: break if jobDoneStatus: continue while self.writeBusy==1: time.sleep(1) while self.readBusy==1: time.sleep(1) if self.readBusy==0: break self.readBusy = 1 time.sleep(1) if jobId in bsubJobList: bsubJobList.remove(jobId) self.readBusy = 0 break
最新发布
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值