timmy / 基於 WebSocket 的遠端指令執行服務
0 喜欢
0 派生
1 文件
最后活跃于 10 months ago
這段程式碼建立了一個 WebSocket 伺服器,允許遠端用戶端傳送指令,並在伺服器端執行該指令後回傳執行結果。適用於遠端系統管理、指令控制或測試環境中的即時互動,但因為直接執行來自用戶端的指令,需特別注意安全性風險,如權限控管與輸入驗證,以防止指令注入攻擊。
| 1 | import asyncio |
| 2 | import websockets |
| 3 | import subprocess |
| 4 | |
| 5 | async def handle_command(websocket, path): |
| 6 | try: |
| 7 | async for message in websocket: |
| 8 | print(f"Received command: {message}") |
| 9 | |
| 10 | # 執行系統指令 |
timmy / 使用 Streamlit 建立 WebSocket 伺服器與客戶端
0 喜欢
0 派生
1 文件
最后活跃于 10 months ago
| 1 | import asyncio |
| 2 | import websockets |
| 3 | import streamlit as st |
| 4 | import threading |
| 5 | import socket |
| 6 | |
| 7 | # Function to check if a port is free |
| 8 | def is_port_free(port): |
| 9 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 10 | return s.connect_ex(('localhost', port)) != 0 |
上一页
下一页