This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
#-*- coding: utf-8 -*- | |
""" | |
包含 yield 指令的函数执行的时候就是一个生成器, 其实一个协程对象, | |
启动后执行到 yield 指令后返回 yield 后的内容, 然后让出, 等待下一次执行. | |
""" | |
def create_generator(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/env python | |
import urllib | |
import contextlib | |
with contextlib.closing(urllib.urlopen("http://www.baidu.com")) as x: | |
print x.code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
#-*- coding: utf-8 -*- | |
""" 此脚本作为参考: | |
根据私钥增加 DNS 正向和反向记录. | |
""" | |
import os |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# Tested with Python 2.7.9, Linux & Mac OS X | |
import socket | |
import StringIO | |
import sys | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import threading | |
import functools | |
global mutex | |
mutex = threading.Lock() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#-*- coding: utf-8 -*- | |
""" | |
output: | |
execute now1(): | |
2013-12-25 | |
call now2(): | |
2013-12-25 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# -*- coding: utf-8 -*- | |
""" partial 实例. | |
看上面的源代码, 可以看出 partial 接受一个函数和若干参数, 返回一个新的函数,新的函数已经包含了之前的函数和参数, | |
而新函数执行时引入的新参数会和之前的参数相加, 真正执行的函数还是之前的函数. | |
所以, partial 的作用是动态绑定参数, 第一次用 partial 生成新函数, 第二次继续绑定参数并执行. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test 是一个目录: | |
test | |
├── __init__.py | |
└── echo.py | |
extend_path 会扫描 sys.path 下面的每一个目录, 如果目录下存在 __name__ 这个子目录, | |
而且子目录下存在 __init__.py 文件, 那么就把子目录加入 __path__ ; | |
而且 它会扫描 sys.path 的目录下是否有 __name__.pkg 文件, 如果有会把里面的路径都加到 __path__(不做检查). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" Nginx 配置文件发布. | |
支持使用 lvs http check 发布和 reload 发布两种方式. | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import tornado.ioloop | |
import tornado.web | |
import tornado.httpserver | |
def _Exception(func): | |
def _decorator(*args, **kwargs): |