commit
2f56f4d13e
@ -0,0 +1,161 @@ |
||||
# ---> Python |
||||
# Byte-compiled / optimized / DLL files |
||||
__pycache__/ |
||||
*.py[cod] |
||||
*$py.class |
||||
|
||||
# C extensions |
||||
*.so |
||||
.idea/ |
||||
# Distribution / packaging |
||||
.Python |
||||
build/ |
||||
develop-eggs/ |
||||
dist/ |
||||
downloads/ |
||||
eggs/ |
||||
.eggs/ |
||||
lib/ |
||||
lib64/ |
||||
parts/ |
||||
sdist/ |
||||
var/ |
||||
wheels/ |
||||
share/python-wheels/ |
||||
*.egg-info/ |
||||
.installed.cfg |
||||
*.egg |
||||
MANIFEST |
||||
|
||||
# PyInstaller |
||||
# Usually these files are written by a python script from a template |
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it. |
||||
*.manifest |
||||
*.spec |
||||
|
||||
# Installer logs |
||||
pip-log.txt |
||||
pip-delete-this-directory.txt |
||||
|
||||
# Unit test / coverage reports |
||||
htmlcov/ |
||||
.tox/ |
||||
.nox/ |
||||
.coverage |
||||
.coverage.* |
||||
.cache |
||||
nosetests.xml |
||||
coverage.xml |
||||
*.cover |
||||
*.py,cover |
||||
.hypothesis/ |
||||
.pytest_cache/ |
||||
cover/ |
||||
|
||||
# Translations |
||||
*.mo |
||||
*.pot |
||||
|
||||
# Django stuff: |
||||
*.log |
||||
local_settings.py |
||||
db.sqlite3 |
||||
db.sqlite3-journal |
||||
|
||||
# Flask stuff: |
||||
instance/ |
||||
.webassets-cache |
||||
|
||||
# Scrapy stuff: |
||||
.scrapy |
||||
|
||||
# Sphinx documentation |
||||
docs/_build/ |
||||
|
||||
# PyBuilder |
||||
.pybuilder/ |
||||
target/ |
||||
|
||||
# Jupyter Notebook |
||||
.ipynb_checkpoints |
||||
|
||||
# IPython |
||||
profile_default/ |
||||
ipython_config.py |
||||
|
||||
# pyenv |
||||
# For a library or package, you might want to ignore these files since the code is |
||||
# intended to run in multiple environments; otherwise, check them in: |
||||
# .python-version |
||||
|
||||
# pipenv |
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. |
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies |
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not |
||||
# install all needed dependencies. |
||||
#Pipfile.lock |
||||
|
||||
# poetry |
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. |
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more |
||||
# commonly ignored for libraries. |
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control |
||||
#poetry.lock |
||||
|
||||
# pdm |
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. |
||||
#pdm.lock |
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it |
||||
# in version control. |
||||
# https://pdm.fming.dev/#use-with-ide |
||||
.pdm.toml |
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm |
||||
__pypackages__/ |
||||
|
||||
# Celery stuff |
||||
celerybeat-schedule |
||||
celerybeat.pid |
||||
|
||||
# SageMath parsed files |
||||
*.sage.py |
||||
|
||||
# Environments |
||||
.env |
||||
.venv |
||||
env/ |
||||
venv/ |
||||
ENV/ |
||||
env.bak/ |
||||
venv.bak/ |
||||
|
||||
# Spyder project settings |
||||
.spyderproject |
||||
.spyproject |
||||
|
||||
# Rope project settings |
||||
.ropeproject |
||||
|
||||
# mkdocs documentation |
||||
/site |
||||
|
||||
# mypy |
||||
.mypy_cache/ |
||||
.dmypy.json |
||||
dmypy.json |
||||
|
||||
# Pyre type checker |
||||
.pyre/ |
||||
|
||||
# pytype static type analyzer |
||||
.pytype/ |
||||
|
||||
# Cython debug symbols |
||||
cython_debug/ |
||||
|
||||
# PyCharm |
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can |
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore |
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear |
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. |
||||
#.idea/ |
@ -0,0 +1,5 @@ |
||||
{ |
||||
"host": "192.168.0.107", |
||||
"port": 8000, |
||||
"key": "096a5d52-6387-4967-8359-46a62acf5a43" |
||||
} |
Binary file not shown.
@ -0,0 +1,59 @@ |
||||
import json |
||||
import asyncio |
||||
import websockets |
||||
import pyperclip |
||||
import sys, os |
||||
import time |
||||
import subprocess |
||||
|
||||
# 获取机器唯一ID,仅适用于Windows系统 |
||||
def get_machine_guid(): |
||||
if sys.platform == "win32": |
||||
cmd = r'reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography /v MachineGuid' |
||||
guid = subprocess.check_output(cmd, shell=True).decode().split()[-1] |
||||
return guid |
||||
return None |
||||
|
||||
# 加载或创建配置文件,配置文件包含连接WebSocket服务器所需的信息 |
||||
def get_config(): |
||||
config_file = 'config.json' |
||||
if not os.path.exists(config_file): |
||||
config = { |
||||
'host': '127.0.0.1', |
||||
'port': 8000, |
||||
'key': get_machine_guid() or "default_key" # 使用机器GUID或默认键作为标识 |
||||
} |
||||
with open(config_file, 'w', encoding='utf-8') as f: |
||||
json.dump(config, f, indent=2) |
||||
else: |
||||
with open(config_file, 'r', encoding='utf-8') as f: |
||||
config = json.load(f) |
||||
return config |
||||
|
||||
# 异步函数,用于持续发送剪贴板数据到服务器 |
||||
async def send_clipboard_data(uri, key): |
||||
previous_clipboard_content = "" # 用于追踪剪贴板内容变化 |
||||
async with websockets.connect(uri) as websocket: # 异步连接WebSocket服务器 |
||||
while True: |
||||
clipboard_content = pyperclip.paste() # 获取当前剪贴板内容 |
||||
if clipboard_content and clipboard_content != previous_clipboard_content: |
||||
previous_clipboard_content = clipboard_content # 更新最新的剪贴板内容 |
||||
data = { |
||||
'key': key, |
||||
'value': json.dumps({"data": clipboard_content, |
||||
'platform': sys.platform, |
||||
'time': time.strftime('%Y-%m-%d %H:%M:%S')}, ensure_ascii=False) |
||||
} |
||||
await websocket.send(json.dumps(data)) # 发送数据到服务器 |
||||
print(f"已更新数据: {data}") # 打印已发送的数据信息 |
||||
await asyncio.sleep(1) # 每1秒检查一次剪贴板的变化 |
||||
|
||||
if __name__ == "__main__": |
||||
config = get_config() |
||||
uri = f"ws://{config.get('host')}:{config.get('port')}/ws" # 构建WebSocket连接URI |
||||
|
||||
try: |
||||
print("正在连接到WebSocket服务器...") |
||||
asyncio.get_event_loop().run_until_complete(send_clipboard_data(uri, config.get("key"))) |
||||
except Exception as e: |
||||
print(f"发生错误: {e}") # 打印任何异常信息 |
Loading…
Reference in new issue