1 Star 0 Fork 0

gsw945/site-clumsy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
print_log_client.py 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
gsw945 提交于 2017-10-08 05:40 . 增强日志功能
# -*- coding: utf-8 -*-
import socket
import time
import traceback
import os
class LogClient(object):
"""docstring for LogClient"""
def __init__(self, port=None):
super(LogClient, self).__init__()
if port is None:
port = 10909
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.addr = ('127.0.0.1', port)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.sock.close()
def write(self, msg):
if not isinstance(msg, bytes):
msg = msg.encode('utf-8', 'ignore')
send = self.sock.sendto(msg, self.addr)
# print(send)
def print_log(msg, port=None):
try:
with LogClient(port) as lc:
# print(msg, file=lc, end='')
lc.write(msg)
except Exception as e:
traceback.print_exc()
print(msg)
if __name__ == '__main__':
for i in range(10):
print(i)
msg = 'log client msg\n msg [{0}]'.format(i)
print_log(msg)
time.sleep(1)
'''
class Test(object):
def __init__(self):
super(Test, self).__init__()
print('print __init__')
def __enter__(self):
print('print __enter__')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print('print __exit__')
'''
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/gsw945/site-clumsy.git
git@gitee.com:gsw945/site-clumsy.git
gsw945
site-clumsy
site-clumsy
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385