Spaces:
Running
Running
| FROM python:3-alpine | |
| ENV M3U_URL=https://raw.githubusercontent.com/best-fan/iptv-sources/master/cn_all.m3u8 | |
| COPY <<'APP' /app.py | |
| import urllib.request, threading, time, os | |
| from http.server import HTTPServer, BaseHTTPRequestHandler | |
| M3U_URL = os.environ.get('M3U_URL', 'https://raw.githubusercontent.com/best-fan/iptv-sources/master/cn_all.m3u8') | |
| content = "" | |
| def refresh(): | |
| global content | |
| while True: | |
| try: | |
| with urllib.request.urlopen(M3U_URL, timeout=30) as r: | |
| content = r.read().decode() | |
| print(f"[IPTV] 源已更新:{len(content)} 字节") | |
| except Exception as e: | |
| print(f"[IPTV] 更新失败:{e}") | |
| time.sleep(43200) # 12小时 | |
| class Handler(BaseHTTPRequestHandler): | |
| def do_GET(self): | |
| if self.path == '/iptv': | |
| self.send_response(200) | |
| self.send_header('Content-Type', 'audio/x-mpegurl') | |
| self.send_header('Access-Control-Allow-Origin', '*') | |
| self.end_headers() | |
| self.wfile.write(content.encode()) | |
| else: | |
| self.send_response(404) | |
| self.end_headers() | |
| def log_message(self, *a): pass | |
| t = threading.Thread(target=refresh, daemon=True) | |
| t.start() | |
| time.sleep(5) | |
| HTTPServer(('0.0.0.0', 50086), Handler).serve_forever() | |
| APP | |
| EXPOSE 50086 | |
| CMD ["python", "/app.py"] | |