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来操作文件. 配置文件的格式: 使用"[]"内包含 ...
随机推荐
- Log4Net使用方法
项目里都会用到日志记录..特别是在本地跑的欢畅..一上服务器就嗝屁的时候..日志会帮你大忙.. Log4net算是一种应用的最广泛的日志记录方式了..下面来简略的说下他的用法.. 先下载log4net ...
- apt-get 与 yum的区别 (转)
一般来说著名的linux系统基本上分两大类:1.RedHat系列:Redhat.Centos.Fedora等2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包格式 ...
- inet_ntoa、 inet_aton、inet_addr
inet_addr() 简述:将一个点间隔地址转换成一个in_addr. #include <winsock.h> unsigned long PASCAL FAR inet_addr ...
- php对uploads文件的处理问题的解决
解决uploads问题的要点有几点: 参考这篇文章 第一, 在php.ini文件中, 有file_uploads这一节 file_uploads = On ;是否开启文件上传功能, 该功能有很大的安全 ...
- 2015年12月03日 GitHub入门学习(五)Markdown语法简介
Markdown一种标记语言,语法简洁,不像Word或Pages有大量排版.字体设置.常用的标记符号不超过十个.被大量写作爱好者.撰稿人.作家所青睐. 一.Markdown的优点 专注你的文字内容而不 ...
- godaddy空间的sql server数据库没办法insert中文
原来的代码: use string007 from sysobjects where id = object_id('K_V_TEST') and type = 'U') drop table K_V ...
- (2)apply函数及其源码
本文原创,转载请注明出处,本人Q1273314690(交流学习) 总结: 就是MARGIN决定了你的FUN调用几次,每次传递给你的是什么维度的内容,而...是传递给FUN的(每次调用的时候都会被传 ...
- gdb调试core文件
本人最近正在学习调试技术,此处对栈溢出做一些总结. gdb的基本使用就不多扯了. 主要针对发行在外的release版本的软件出现问题时的调试. 一般来讲,查看堆栈就是使用bt,这个时候加上bt ful ...
- cf558c(bfs)
C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Logistic 回归(sigmoid函数,手机的评价,梯度上升,批处理梯度,随机梯度,从疝气病症预测病马的死亡率
(手机的颜色,大小,用户体验来加权统计总体的值)极大似然估计MLE 1.Logistic回归 Logistic regression (逻辑回归),是一种分类方法,用于二分类问题(即输出只有两种).如 ...