代码拉取完成,页面将自动刷新
import asyncio
import websockets
import websockets_routes
import datetime
from kubernetes import client, config
import json
import pytz
import asyncio
import websockets
config.kube_config.load_kube_config(config_file="/root/.kube/config")
core = client.CoreV1Api()
api = client.AppsV1Api()
class AsyncLog1:
def __init__(self, items):
self.items = items
async def __aiter__(self):
for item in self.items:
yield item
class AsyncLog2:
def __init__(self, seq):
self.iter = iter(seq)
def __aiter__(self):
return self
# for item in self.items:
# yield item
async def __anext__(self):
try:
return next(self.iter)
except StopIteration:
raise StopAsyncIteration
def read_pod_container_log_stream(namespace, name, container="", number=0):
log_stream = core.read_namespaced_pod_log(
name=name,
namespace=namespace,
container=container,
follow=True,
pretty=True,
_preload_content=False,
timestamps=True,
tail_lines=number
).stream()
return log_stream
class AsyncLog3:
def __init__(self, namespace, name):
self.iter = iter(read_pod_container_log_stream(namespace, name))
def __aiter__(self):
return self
# for item in self.items:
# yield item
async def __anext__(self):
try:
return next(self.iter)
except StopIteration:
raise StopAsyncIteration
async def ws_handler(websocket):
async for i in AsyncLog3("default", "hello-python-5885fc4968-788s8"):
if i:
# await websocket.send(i.decode("utf-8"))
log_line = i.decode("utf-8").replace("\n", "\r\n")
await websocket.send(log_line)
command = await websocket.recv()
print(command)
async def main():
async with websockets.serve(ws_handler, "0.0.0.0", 8765):
await asyncio.Future()
asyncio.run(main())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。