Python writes a WC command for sending and receiving data, and more learning communication please add 365534424 , directly on the code
#!/usr/bin/env python# coding:utf-8# author: 51reboot.com# qq Group:365534424from OPTPARSE IMPORT OPTIONPARSERIMPORT OSCLASS WC (object): def __ Init__ (self): self.n = 0 # line num self.error = ' def run (self): self.options = main () if self.options.l: # wc -l if Self.get_line (): print self.n, self.options.filename else: print self.error def get_line (self): if Self.options.filename and os.path.exists (Self.options.filename) : try: with open (Self.options.filename) as f: for line in f: self.n +=1 return True except IOError: self.error = ' File not readable ' return False else: self.error = ' file does not exist ' return False def Main (): parser = optionparser () parser.add_option ('-l ') , help= ' Print the length of the longest line ', action= ' store_true ') (Options,args) = parser.parse_args () if len (args) == 1: options.filename = args[0] else: options.filename = ' return optionsif __name__ == ' __main__ ':     WC = WC () wc.run ()
This article is from the "Reboot DevOps Development" blog, please be sure to keep this source http://opsdev.blog.51cto.com/2180875/1569081
Python wrote a WC command.