python+webrtc实现p2p大文件传输示例代码

代码语言:python

所属分类:其他

代码描述:python+webrtc实现p2p大文件传输示例代码,使用fastapi内嵌html,在浏览器中打开一个网页就能相互传输大文件。

代码标签: python webrtc p2p 大文件 传输 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

import asyncio
import json
import logging
import uuid
from typing import Dict, List

import uvicorn
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse

# 配置日志
logging.basicConfig(level=logging.INFO)

app = FastAPI()

# 存储房间信息
# 结构: {"room_id": {"creator": WebSocket, "users": [WebSocket, ...]}}
rooms: Dict[str, Dict] = {}

# HTML模板 (JavaScript部分已修改以使用原生WebSocket)
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SecureShare - P2P文件传输 (FastAPI版)</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; display: flex; align-items: center; justify-content: center; color: #333; }
        .container { background: rgba(255, 255, 255, 0.95); backdrop-filter: blur(10px); border-radius: 20px; padding: 40px; box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1); max-width: 800px; width: 90%; min-height: 600px; }
        .header { text-align: center; margin-bottom: 40px; }
        .logo { font-size: 2.5rem; font-weight: 700; background: linear-gradient(45deg, #667eea, #764ba2); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin-bottom: 10px; }
        .subtitle { color: #666; font-size: 1.1rem; margin-bottom: 30px; }
        .tabs { display: flex; border-radius: 12px; background: #f8f9fa; padding: 4px; margin-bottom: 30px; }
        .tab { flex: 1; padding: 12px 20px; text-align: center; border-radius: 8px; cursor: pointer; transition: all 0.3s ease; font-weight: 500; }
        .tab.active { background: linear-gradient(45deg, #667eea, #764ba2); color: white; box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3); }
        .tab-content { display: none; }
        .tab-content.active { display: block; }
        .form-group { margin-bottom: 25px; }
        label { display: block; margin-bottom: 8px; font-weight: 600; color: #333; }
        input[type="text"], input[type="file"] { width: 100%; padding: 15px; border: 2px solid #e1e5e9; border-radius: 10px; font-size: 16px; transition: border-color 0.3s ease; }
        input[type="text"]:focus, input[type="file"]:focus { outline: none; border-color: #667eea; }
        .btn { background: linear-gradient(45deg, #667eea, #764ba2); color: white; border: none; padding: 15px 30px; border-radius: 10px; font-size: 16px; font-weight: 600; cursor: pointer; transition: transform 0.2s ease, box-shadow 0.2s ease; width: 100%; }
        .btn:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3); }
        .btn:disabled { background: #ccc; cursor: not-allowed; transform: none; box-shadow: none; }
        .connection-info { background: #f8f9fa; border-radius: 10px; padding: 20px; margin: 20px 0; text-align: center; }
        .room-id { font-size: 24px; font-weight: 700; color: #667eea; margin: 10px 0; letter-spacing: 2px; }
        .status { padding: 10px 20px; border-radius: 20px; font-weight: 600; margin: 10px 0; display: inline-block; }
        .status.waiting { background: #fff3cd; color: #856404; }
        .status.connected { background: #d4edda; color: #155724; }
        .status.error { background: #f8d7da; color: #721c24; }
        .progress-container { background: #f8f9fa; border-radius: 10px; padding: 20px; margin: 20px 0; display: none; }
        .progress-bar { width: 100%; height: 8px; background: #e1e5e9; border-radius: 4px; overflow: hidden; }
        .progress-fill { height: 100%; background: linear-gradient(45deg, #667eea, #764ba2); width: 0%; transition: width 0.3s ease; }
        .progress-text { text-align: center; margin-top: 10px; font-weight: 600; }
        .file-info { background: #f8f9fa; border-radius: 10px; padding: 15px; margin: 15px 0; display: none; }
        .file-name { font-weight: 600; margin-bottom: 5px; }
        .file-size { color: #666; font-size: 14px; }
        @media (max-width: 768px) { .container { padding: 20px; margin: 20px; } .logo { font-size: 2rem; } }
    </style>
</head>
<body>
    <div class="container">
        <!-- HTML结构保持不变 -->
        <div class="header">
            <div class="logo">SecureShare</div>
            <div class="subtitle">端到端加密的P2P文件传输平台</div>
        </div>

        <div class="tabs">
            <div class=&quo.........完整代码请登录后点击上方下载按钮下载查看

网友评论0