Python的函数定义中有两种特殊的情况,即出现*,**的形式。
如:def myfun1(username, *keys)或def myfun2(username, **keys)等。

他们与函数有关,在函数被调用时和函数声明时有着不同的行为。此处*号不代表C/C++的指针。

其中 * 表示的是元祖或是列表,而 ** 则表示字典

第一种方式:

 import httplib
def check_web_server(host,port,path):
h = httplib.HTTPConnection(host,port)
h.request('GET',path)
resp = h.getresponse()
print 'HTTP Response'
print ' status =',resp.status
print ' reason =',resp.reason
print 'HTTP Headers:'
for hdr in resp.getheaders():
print ' %s : %s' % hdr if __name__ == '__main__':
http_info = {'host':'www.baidu.com','port':'','path':'/'}
check_web_server(**http_info)

第二种方式:

 def check_web_server(**http_info):
args_key = {'host','port','path'}
args = {}
#此处进行参数的遍历
#在函数声明的时候使用这种方式有个不好的地方就是 不能进行 参数默认值
for key in args_key:
if key in http_info:
args[key] = http_info[key]
else:
args[key] = '' h = httplib.HTTPConnection(args['host'],args['port'])
h.request('GET',args['path'])
resp = h.getresponse()
print 'HTTP Response'
print ' status =',resp.status
print ' reason =',resp.reason
print 'HTTP Headers:'
for hdr in resp.getheaders():
print ' %s : %s' % hdr if __name__ == '__main__':
check_web_server(host= 'www.baidu.com' ,port = '',path = '/')
http_info = {'host':'www.baidu.com','port':'','path':'/'}
check_web_server(**http_info)

转载来自:http://my.oschina.net/u/1024349/blog/120298

最新文章

  1. Android屏幕适配
  2. 19. 求平方根序列前N项和
  3. abap append 用法
  4. Spring入门(6)-使用注解装配
  5. Js setInterval与setTimeout(定时执行与循环执行)的代码(可以传入参数)
  6. 连载:面向对象葵花宝典:思想、技巧与实践(32) - LSP原则
  7. Hibernate框架增删改查测试类归为一个类
  8. 2014.9.25DOM元素操作
  9. 《C++ Primer》之重载操作符与转换(下)
  10. Delphi XE7 Update1修正列表
  11. 【php增删改查实例】第二十二节 - 引入百度地图
  12. java-null简介
  13. Nginx一台机器上负载均衡多个Tomcat
  14. centos7.2 部署zabbix 3.2.7
  15. python细节问题
  16. layer mobile 指定URL连接 弹全屏
  17. 下厨房6月26日数据丢失事故总结 MYSQL主分区被rm 命令误删除
  18. azkaban:java任务调度系统
  19. go日期时间函数+常用内建函数+错误处理
  20. MDN搜索结果自动跳转中文地址

热门文章

  1. SVN迁移到Git的过程(+ 一些技巧)
  2. SVN迁移到Git的过程(+ 一些技巧
  3. PHP--获取响应头(Response Header)方法
  4. 第七天 面向对象进阶与socket编程
  5. 【转】WebMagic-总体流程源码分析
  6. linux日常小坑
  7. ACM/ICPC 之 DP进阶(51Nod-1371(填数字))
  8. 7.js模式-装饰者模式
  9. 关于TortoiseSVN的一些知识
  10. ffmpeg-20160726-bin.7z