vue项目接口域名动态的获取方法
246
2022-08-27
Python从门到精通(一):基础-附-开发一个python脚本(python编程从入门到实践 豆瓣)
一、基础内容
本章中介绍一个编写脚本可能用到的一个方法
1.1、脚本接收
import fileinputwith fileinput.input('/etc/passwd') as f: for line in f: print(f.filename(), f.lineno(), line, end='')
1.2、程序退出
import syssys.stderr.write('It failed!\n')raise SystemExit(1)
1.3、密码验证
import getpassuser = getpass.getuser()#这行会提示输入明文密码,是否明文这与系统有关passwd = getpass.getpass()# for show the use of getpassdef svc_login(user, passwd): return Trueif svc_login(user, passwd): print('Success!')else: print('Fail!')user = input('Enter your username: ')
1.5、执行Linux命令
import subprocessout_bytes = subprocess.check_output(['netstat','-a'])out_text = out_bytes.decode('utf-8')#异常处理try: out_bytes = subprocess.check_output(['cmd','arg1','arg2'])except subprocess.CalledProcessError as e: out_bytes = e.output # Output generated before error code = e.returncode # Return code#同时收集标准输出和错误out_bytes = subprocess.check_output(['cmd','arg1','arg2'], stderr=subprocess.STDOUT)#设置超时机制try: out_bytes = subprocess.check_output(['cmd','arg1','arg2'], timeout=5)except subprocess.TimeoutExpired as e: ...#让命令在shell环境中执行out_bytes = subprocess.check_output('grep python | wc > out', shell=True)#如需更复杂的交互可使用pexpect模块
1.6、打开浏览器
import webbrowserwebbrowser.open('= webbrowser.get('chrome')c.open('signalimport resourceimport osdef time_exceeded(signo, frame): print("Time's up!") raise SystemExit(1)def set_max_runtime(seconds): # Install the signal handler and set a resource limit soft, hard = resource.getrlimit(resource.RLIMIT_CPU) resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard)) signal.signal(signal.SIGXCPU, time_exceeded)if __name__ == '__main__': set_max_runtime(20) while True: passdef limit_memory(maxsize): soft, hard = resource.getrlimit(resource.RLIMIT_AS) resource.setrlimit(resource.RLIMIT_AS, (maxsize, hard))if __name__ == '__main__': limit_memory(1) set_max_runtime(20) while True: pass
三、命令行解析器
执行 python orderParser.py -h 输出如下:
usage: order_parser.py [-h] -p pattern [-v] [-o OUTFILE] [--speed {slow,fast}] [filename ...]Search some filespositional arguments: filenameoptional arguments: -h, --help show this help message and exit -p pattern, --pat pattern text pattern to search for -v verbose mode -o OUTFILE output file --speed {slow,fast} search speed
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~