码迷,mamicode.com
首页 > 其他好文 > 详细

命令行参数

时间:2020-09-04 17:10:31      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:must   lse   put   read   oca   add   one   通过   name   

通过管道传递

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)

命令行参数

标签:must   lse   put   read   oca   add   one   通过   name   

原文地址:https://www.cnblogs.com/zhaobowen/p/13567902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!