The optparse of the Python module

Source: Internet
Author: User

Reference:

    • Http://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.html
    • Https://docs.python.org/2/library/optparse.html

eg

#This is the blocking Get poetry now! client.Importdatetime, Optparse, SocketdefParse_args (): Usage="""usage:%prog [options] [hostname]:p ort ... This is the Get poetry now! Client, blocking edition. Run it like This:python get-poetry.py port1 port2 port3 ... If you is in the base directory of the Twisted-intro package,you could run it like This:python Blocking-client/get-poet ry.py 10001 10002 10003to grab poetry from servers on ports 10001, 10002, and 10003.Of course, there need to be servers Li Stening on those portsfor."""Parser=Optparse. Optionparser (usage) _, addresses=Parser.parse_args ()if  notaddresses:Printparser.format_help () parser.exit ( )defparse_address (addr):if ':'  not inchAddr:host='127.0.0.1'Port=AddrElse: Host, Port= Addr.split (':', 1)        if  notport.isdigit (): Parser.error ('Ports must be integers.')        returnhost, int (port)returnmap (parse_address, addresses)defGet_poetry (address):"""Download a piece of poetry from the given address."""sock=Socket.socket (socket.af_inet, socket. SOCK_STREAM) Sock.connect (address) poem="'     whileTrue:#This was the ' blocking ' call in this synchronous program.        #The recv () method would block for an indeterminate period        #Of time waiting for bytes to is received from the server.Data= SOCK.RECV (1024)        if  notdata:sock.close () BreakPoem+=DatareturnPoemdefformat_address (address): Host, Port=Addressreturn '%s:%s'% (hostor '127.0.0.1', Port)defmain (): Addresses=Parse_args () elapsed=Datetime.timedelta () forI, AddressinchEnumerate (addresses): #http://blog.csdn.net/suofiya2008/article/details/5603861 addr_fmt=format_address (address)Print 'Task%d:get poetry from:%s'% (i + 1), addr_fmt) Start=Datetime.datetime.now ()#Each execution of ' get_poetry ' corresponds to the        #execution of one synchronous task in Figure 1 here:        #Http://krondo.com/?p=1209#figure1Poem=get_poetry (address) time= Datetime.datetime.now ()-Start msg='Task%d:got%d bytes of poetry from%s in%s'        PrintMsg% (i + 1), Len (poem), addr_fmt, time) elapsed+= TimePrint 'Got%d poems in%s'%(Len (addresses), elapsed)if __name__=='__main__': Main ()

The optparse of the Python module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.