Python模块之optparse
参考:
- 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, socket def 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 10003 to 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 = addr
else:
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()
break poem += data return poem def format_address(address):
host, port = address
return '%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/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#figure1 poem = get_poetry(address) time = datetime.datetime.now() - start msg = 'Task %d: got %d bytes of poetry from %s in %s'
print msg % (i + 1, len(poem), addr_fmt, time) elapsed += time print 'Got %d poems in %s' % (len(addresses), elapsed) if __name__ == '__main__':
main()
Python模块之optparse的更多相关文章
- Python模块学习——optparse
Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...
- python 模块 optparse
optparse,是一个能够让程式设计人员轻松设计出简单明了.易于使用.符合标准的Unix命令列程式的Python模块.生成使用和帮助信息. 下面是一个简单的示例: import optparse p ...
- Day11 - Python基础11 模块学习——optparse
Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...
- python模块大全
python模块大全2018年01月25日 13:38:55 mcj1314bb 阅读数:3049 pymatgen multidict yarl regex gvar tifffile jupyte ...
- [转]Python 模块收集
Python 模块收集 转自:http://kuanghy.github.io/2017/04/04/python-modules Python | Apr 4, 2017 | python 工具 a ...
- Python模块: 命令行解析optionparser
Python 有两个内建的模块用于处理命令行参数:一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数:另一个是 optparse,它功能强大,而 ...
- Python - 模块(一)
目录 Python - 模块(一) 模块的引用方式 常用模块 random(随机模块) os模块 sys 序列化模块 hashlib subprocess optparse struct Python ...
- 使用C/C++写Python模块
最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...
- Python模块之configpraser
Python模块之configpraser 一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...
随机推荐
- PHPCMS 模板制作标签
内容模块: 栏目调用1: {pc:content action="category" catid="0" num="25" siteid=& ...
- <Web 之困 现代Web应用安全指南>一本好书 69.00?
NET代码安全 界面漏洞防范与程序优化 一. SQL 注入攻击的源头 1. 过滤或转移危险字符 2. 使用SqlParameter类:.NET 框架有一个叫做SqlParameter 的集合类型,可 ...
- 缺陷跟踪系统Mantis Bug Tracker
缺陷管理平台Mantis,也做MantisBT,全称Mantis Bug Tracker. 项目在github的地址:https://github.com/mantisbt/mantisbt Mant ...
- [CentOs7]图形界面
摘要 为了更方面的看到命令的执行后的效果,感觉安装一个图形界面,学习起来更有感觉.至少知道自己做了哪些事.在刚开始安装虚机的时候,选择了最小安装centos7,发现在使用命令安装图形界面的时候,尝试了 ...
- Sublime Text 2 快捷键 (windows)
转自:http://istyles.blog.163.com/blog/static/1811003892011828111418654/ Lucifr翻译了 Sublime Text 2 快捷键 M ...
- JDBC:从数据库中取数据的一个bug
先看错误信息: java.sql.SQLException: Before start of result set at com.mysql.jdbc.SQLError.createSQLExcept ...
- linux命令行netstat总结
1.所谓的监听就是某个服务程序会一直常驻在内存中,所以该程序启动的Port就会一直存在. 2.在小于1023的端口,都是需要以root身份才能够启动的. 3.大于1024以上的Port主要是作为cli ...
- iOS 滑动性能优化
iOS 滑动性能优化 目录 一. 减少图层的Blend操作 1. UIView的背景色避免使用clearColor 2. 控件贴图避免使用带alpha的图片 3. UIImageView 使用时避免半 ...
- 黄学长模拟day1 大逃亡
给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上,矩形的行号从0到X-1,列号从 ...
- cocos布局分析
HBox和VBox布局 HBox只是一个水平布局包装类. HBox里面所有的孩子节点都会水平排列成一行 VBox仅仅是对垂直布局的一个简便的类封装. VBox把它的子节点布局在一竖列中. Layout ...