参考:

  • 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

Python模块之optparse相关推荐

  1. Python模块学习——optparse

    Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...

  2. Python模块学习-----optparse

    作用:一种类似于dos命令行的一种设计模块,例如:dos命令中添加-f会触发一种事件           可以自定义一些指令,用来完成某些操作 optparse.OptionParser() opti ...

  3. Python中的 optparse模块

    python的内置模块中对于命令行的解析模块共两个getopt 和 optparse .不过getopt过于简单,往往不能满足需求.此时可以使用optparse模块.这个模块相对于getopt更新,功 ...

  4. Python下使用optparse模块实现对多个文件进行统计【二】

    一个取代shell wc -l 命令的python小脚本 1.通过python下optparse模块下OptionParser类是新对文件的统计 #!/opt/data/ipy/bin/python ...

  5. Python模块: 命令行解析optionparser

    Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...

  6. python 模块 导入机制 模块搜索 Python包 发布python模块或程序

    python 模块 python模块:以.py结尾的代码文件.        顶层文件: 程序执行入口        模块文件1        模块文件2        模块文件3 在python中一 ...

  7. Python3,我用这种方式讲解python模块,80岁的奶奶都说能理解。建议收藏 ~ ~

    Python模块讲解 1.引言 2.python模块详解 2.1 含义 2.2 代码示例 2.3 进阶 3.总结 1.引言 小屌丝:鱼哥,你看天上的月亮越来越圆了. 小鱼:唉~ 又是一年团圆夜,又是一 ...

  8. Python模块(自己整理并不完整)

    PY核心模块方法 ******************** os模块: os.remove() 删除文件  os.unlink() 删除文件  os.rename() 重命名文件  os.listdi ...

  9. GPUtil是一个Python模块,使用nvidia-smi从NVIDA GPU获取GPU状态

    GPUtil是一个Python模块,使用nvidia-smi从NVIDA GPU获取GPU状态 一个Python模块,用于在Python中使用nvidia-smi以编程方式从NVIDA GPU获取GP ...

最新文章

  1. [Oracle]理解undo表空间
  2. html传递json中文乱码,解决后台传数据到前台中文乱码问题,使用@ResponseBody返回json 中文乱码...
  3. c语言 static 关键字的作用
  4. 几个 PHP 的“魔术常量”
  5. 某项目要调用现有的100多个DLL 一 开始
  6. HIPS 自定义框架
  7. java方向好看的书
  8. 中国电信学院c语言题库,电脑题库试题精编版.doc
  9. [论文阅读][SLAM]Targetless Calibration of LiDAR-IMU System Based on Continuous-time Batch Estimation
  10. Code::Blocks 16.01 改变注释的的颜色
  11. iOS OC与JS交互(WebView监听事件)
  12. 归心似箭,IT达人分享抢票攻略
  13. 6,JESD204B接口简介
  14. android bugly 错误分析,Android使用bugly捕捉异常
  15. Lock wait timeout exceeded
  16. css pseudo elements,css伪元素(Pseudo-elements)各种使用类型
  17. Android 热敏打印机打印二维码(转载)
  18. Python筛选某列满足条件的值(isin用法)
  19. JS数组常用方法练习题
  20. python程序设计入门书籍推荐_python刚刚入门,接下来这几本python的书会让你成为别人眼里的大神!...

热门文章

  1. javacript Function parameters(函数参数)
  2. texstudio如何安装cjk宏包_MikTex+TexStudio配置论文写作环境
  3. 阿里程序员发70多万年终奖表示略感失望,网友:不要的话请给我!
  4. 深度|数据库产品如何选型?掌握这一招就够了
  5. Docker学习总结(33)——Docker环境下搭建 MySQL 主从复制
  6. oracle 11g rman catalog,Oracle 11g RMAN复制数据库的测试
  7. meteor 结合mysql_Meteor集合
  8. 反向微分运放电路波形_做到这三步,轻松实现运放电路稳定性
  9. java jar命令_Java命令行之jar命令
  10. 高项信息系统项目管理师考试大纲——重点知识