Skip to content

Instantly share code, notes, and snippets.

View lwzm's full-sized avatar
🎯
Focusing

Weizhao Li lwzm

🎯
Focusing
View GitHub Profile
@lwzm
lwzm / md5sum.py
Created June 5, 2014 09:26
command `md5sum` in Python
import hashlib
def md5sum(t):
return hashlib.md5(t).hexdigest()
if __name__ == "__main__":
print(md5sum(b'')) # same as "$ md5sum /dev/null" in shell
class Sleep(RequestHandler):
@tornado.web.asynchronous
def get(self):
self.set_header("Content-Type", "text/plain")
self.loop(100)
def loop(self, n):
if not n:
return self.finish()
self.write("{}\n".format(n))

用 nc, curl 观察 http 协议, 和 cat 配合.

nc 充当服务器: nc -l 8000 >http_request curl -v http://localhost:8000 得到 http_request, 与 curl 显示的一致

启动普通的 HTTP 服务器, 用 nc 做客户端: cat http_request - | nc localhost 8000

# tornado:
import datetime
from tornado.web import Application, RequestHandler, asynchronous
from tornado.ioloop import IOLoop
from tornado.options import parse_command_line
io_loop = IOLoop.instance()
delay = datetime.timedelta(milliseconds=3)
class Sleep(RequestHandler):
import redis
db = redis.StrictRedis()
hget = db.hget
hset = db.hset
class Session(object):
def __init__(self, name):
self.name = name
def __getitem__(self, key):
def timeit(func):
from time import time
def wrapper(*args, **kwargs):
t = time()
func(*args, **kwargs)
print(func.__name__, time() - t)
return wrapper
@lwzm
lwzm / logger.py
Last active March 28, 2019 06:22
file-logger like logging.handlers.TimedRotatingFileHandler
import os
import pathlib
import time
import sys
def get_logger(logfilename, logsuffix_timefmt="%y%m%d", patch_stderr=False):
strftime = time.strftime
timefmt = "%y%m%d%H%M%S"
assert timefmt.find(logsuffix_timefmt) == 0, logsuffix_timefmt
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisord]
pidfile = run/supervisord.pid
logfile = log/supervisord.log
[unix_http_server]
file = run/supervisor.sock
chmod = 0766
pallet[1] = 0xFF800000;
pallet[2] = 0xFF008000;
pallet[3] = 0xFF808000;
pallet[4] = 0xFF000080;
pallet[5] = 0xFF800080;
pallet[6] = 0xFF008080;
pallet[7] = 0xFFC0C0C0;
pallet[8] = 0xFF558097;
pallet[9] = 0xFF9DB9C8;
pallet[10] = 0xFF7B7373;
#!/usr/bin/env python3
import time
def time_int_to_str(i):
"""
>>> time_int_to_str(0)
'1970-01-01 08:00:00'
"""