zoukankan      html  css  js  c++  java
  • 命令行参数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)
    

    参考文档

    --------------------------------------------------------- 恐惧是因为努力的还不够,加油 ~~---------------------------------------------
  • 相关阅读:
    翻转单词顺序
    java layout 表格项增加、删除、修改
    Hadoop学习之HBase
    protected的一些功能
    hdu4431 Mahjong
    kendo ui gird温馨提示(使用本地数据) 一个
    TOP计划猿10最佳实践文章
    Java高级应用(一个)-文件夹监控服务
    BSD介绍
    获取编译学习笔记 (十三)—— 外部中断
  • 原文地址:https://www.cnblogs.com/zhaobowen/p/13567902.html
Copyright © 2011-2022 走看看