python opcua模塊,Python模塊之optparse

 2023-10-06 阅读 20 评论 0

摘要:參考: http://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.htmlhttps://docs.python.org/2/library/optparse.htmleg: # This is the blocking Get Poetry Now! client.import datetime, optparse, socketdef parse_args():usage = """u

參考:

  • 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.import datetime, optparse, socketdef parse_args():usage = """usage: %prog [options] [hostname]:port ...This is the Get Poetry Now! client, blocking edition.
Run it like this:python get-poetry.py port1 port2 port3 ...If you are in the base directory of the twisted-intro package,
you could run it like this:python blocking-client/get-poetry.py 10001 10002 10003to grab poetry from servers on ports 10001, 10002, and 10003.Of course, there need to be servers listening on those ports
for that to work.
"""parser = optparse.OptionParser(usage)_, addresses = parser.parse_args()if not addresses:print parser.format_help()parser.exit()def parse_address(addr):if ':' not in addr:host = '127.0.0.1'port = addrelse:host, port = addr.split(':', 1)if not port.isdigit():parser.error('Ports must be integers.')return host, int(port)return map(parse_address, addresses)def get_poetry(address):"""Download a piece of poetry from the given address."""sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.connect(address)poem = ''while True:# This is the 'blocking' call in this synchronous program.# The recv() method will block for an indeterminate period# of time waiting for bytes to be received from the server.
data = sock.recv(1024)if not data:sock.close()breakpoem += datareturn poemdef format_address(address):host, port = addressreturn '%s:%s' % (host or '127.0.0.1', port)def main():addresses = parse_args()elapsed = datetime.timedelta()for i, address in enumerate(addresses):    #http://blog.csdn.net/suofiya2008/article/details/5603861addr_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#figure1
poem = get_poetry(address)time = datetime.datetime.now() - startmsg = 'Task %d: got %d bytes of poetry from %s in %s'print  msg % (i + 1, len(poem), addr_fmt, time)elapsed += timeprint 'Got %d poems in %s' % (len(addresses), elapsed)if __name__ == '__main__':main()

?

轉載于:https://www.cnblogs.com/flowjacky/p/4418432.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/119947.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息