命令行参数parser模块
通过管道传递
cat somefile.txt |python somescriot.py |sort
import sys
text = sys.stdin.read() #标准输入流
words = text.split()
print words
#用户互动
raw_input()
x = input('Enter the first number:')
y = input('Enter the second number:')
参数
判断个数
if len(sys.argv) == 2:
if sys.argv[1] == "check_hottopic":
check_hottopic()
if sys.argv[1] == "check_mongo":
check_mongo()
if sys.argv[1] == "check_menhu":
check_menhu()
if sys.argv[1] == "check_hotdoc":
check_hotdoc()
else:
print (".........the args must be [check_hottopic,check_mongo,check_menhu,check_hotdoc]. ")
else:
print (".........the arg have been more than one or no one found in args. ")
命令行参数
#!/bin/env python
import socket
import re
import sys
def check_server(address,port):
s = socket.socket()
print "Attempting to connect to %s on port %s" % (address,port)
try:
s.connect((address,port))
print "Connected to %s on port %s" % (address,port)
return True
except socket.error,e:
print "Connected to %s on port %s failed:%s" % (address,port,e)
return False
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
#加入参数选项
parser.add_option("-a","--address",dest="address",default='localhost',help="ADDRESS for server",metavar="ADDRESS")
parser.add_option("-p", "--port",dest="port",type="int",default=80,help="PORT for server",metavar="PORT")
#解析
(options,args) = parser.parse_args()
print 'options :%s,args:%s' % (options,args)
check = check_server(options.address,options.port)
print 'check_server returned %s' % check
sys.exit(not check)
---------------------------------------------------------
恐惧是因为努力的还不够,加油 ~~---------------------------------------------