# 一致监听本地剪切板,如果有变化,则打印出来 import threading import time import pyperclip import redis import uvicorn as uvicorn from fastapi import FastAPI from starlette.middleware.cors import CORSMiddleware from starlette.middleware.sessions import SessionMiddleware from starlette.responses import JSONResponse recent_value = pyperclip.paste() r = redis.StrictRedis(host='127.0.0.1', port=6379, db=0) def clipboard_loop(interval): global recent_value recent_value = pyperclip.paste() while True: tmp_value = pyperclip.paste() if tmp_value != recent_value: recent_value = tmp_value r.set('clipboard', recent_value) print(recent_value) time.sleep(interval) app = FastAPI(title="project name1", description="系统描述 ", version="v 0.0.0") app.add_middleware(SessionMiddleware, secret_key='123456hhh') app.add_middleware( CORSMiddleware, allow_origins=["*", ], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # api 获取剪切板 @app.get('/clipboard') def get_clipboard(): return {'clipboard': r.get('clipboard').decode()} @app.exception_handler(Exception) def validation_exception_handler(request, exc): """请求校验异常捕获; application/json """ return JSONResponse({'message': "服务器内部错误", 'status_code': 500}) if __name__ == '__main__': new_thread = threading.Thread(target=clipboard_loop, args=(0.5,)) new_thread.start() uvicorn.run(app='readClient:app', host="0.0.0.0", port=8000)