首页 > 编程语言 > 详细

Python 上下文关系

时间:2016-09-16 11:17:17      阅读:132      评论:0      收藏:0      [点我收藏+]
import socket
import contextlib

@contextlib.contextmanager
def con(host,port):
    sk = socket.socket()
    sk.bind((host,port))
    sk.listen(5)
    try:
        print ("auto connect")
        yield
        print ("============")
    finally:
        print ("finally close")
        sk.close()


with con(127.0.0.1,8888) as sock:
    print ("sock auto close")
执行结果:
auto connect
sock auto close
============
finally close

 

Python 上下文关系

原文:http://www.cnblogs.com/python-study/p/5876024.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!