参考:

  • 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()

最新文章

  1. 微信上传文章素材—ASP.NET MVC从View层传数据到Controller层
  2. weblogic网页登录URL
  3. Flex Flash Player回声消除的最佳方法
  4. UCenter整合登陆时出现’Authorization has expired’错误(2014-03-13记)
  5. BZOJ1845 : [Cqoi2005] 三角形面积并
  6. JDBC工作模块
  7. 关于时间序列数据库的思考——(1)运用hash文件(例如:RRD,Whisper) (2)运用LSM树来备份(例如:LevelDB,RocksDB,Cassandra) (3)运用B-树排序和k/v存储(例如:BoltDB,LMDB)
  8. git使用中遇到的常见问题
  9. IOS面试攻略
  10. Oracle merge into 使用记录
  11. iOS数据持久化 -- Core Data-备用
  12. php集成环境和自己配置的区别,php集成环境、php绿色集成环境、php独立安装版环境这三者的区别
  13. Salt: Master server cannot see any Minion
  14. Java的this关键字在继承时的作用
  15. 离线安装Cloudera Manager 5和CDH5(最新版5.9.3) 完全教程(三)重新分配磁盘空间(可选)
  16. python3之协程
  17. [转]CentOS7 下安装svn
  18. QQ项目(续)
  19. poj 1236 强联通分量
  20. ActiveMQ之HelloWorld

热门文章

  1. Haproxy安装配置及日志输出问题
  2. 【转】asp.net mvc 页面跳转
  3. Linux命令之dos2unix
  4. 在hexo静态博客中利用d3-cloud来展现标签云
  5. hadoop之根据Rowkey从HBase中查询数据
  6. 北美IT公司大致分档
  7. cocos2d-x内存管理(见解)
  8. PHP get_class 返回对象的类名
  9. C++调用shell
  10. maven工程通过命令打包