Spaces:
Running
Running
Download mobile_server.py from bllndebrahim/translet-test: direct link, hf CLI and curl.
- Browser
- Download file 3.47 kB
-
https://huggingface.co/spaces/bllndebrahim/translet-test/resolve/main/mobile_server.py
- Command line
-
hf download hf://spaces/bllndebrahim/translet-test/mobile_server.py
-
curl -L -o mobile_server.py https://huggingface.co/spaces/bllndebrahim/translet-test/resolve/main/mobile_server.py
3.47 kB
| import subprocess | |
| import time | |
| import os | |
| import re | |
| import socket | |
| def get_local_ip(): | |
| try: | |
| s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| s.connect(("8.8.8.8", 80)) | |
| ip = s.getsockname()[0] | |
| s.close() | |
| return ip | |
| except Exception: | |
| return "127.0.0.1" | |
| def is_server_running(port=3000): | |
| try: | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| s.settimeout(1) | |
| res = s.connect_ex(('127.0.0.1', port)) | |
| s.close() | |
| return res == 0 | |
| except Exception: | |
| return False | |
| def main(): | |
| print("=" * 65) | |
| print(" 🚀 Translet Mobile & Public Cloudflare Tunnel") | |
| print("=" * 65) | |
| base_dir = os.path.dirname(os.path.abspath(__file__)) | |
| log_file = os.path.join(base_dir, "tunnel_url.log") | |
| # 1. Ensure Python server.py is running | |
| if not is_server_running(3000): | |
| print("[1/2] سێرڤەری پایتۆن لەسەر پۆڕتی 3000 دەستپێدەکات...") | |
| subprocess.Popen(["python", "server.py", "3000"], cwd=base_dir, creationflags=subprocess.CREATE_NEW_CONSOLE) | |
| time.sleep(2) | |
| else: | |
| print("[1/2] سێرڤەری پایتۆن ئامادەیە لەسەر پۆڕتی 3000.") | |
| # 2. Check cloudflared.exe | |
| cloudflared_path = os.path.join(base_dir, "cloudflared.exe") | |
| if not os.path.isfile(cloudflared_path): | |
| print("داگرتنی cloudflared.exe...") | |
| subprocess.run(["curl.exe", "-L", "-o", cloudflared_path, "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe"]) | |
| print("[2/2] بەستنەوە بە هێڵی ئینتەرنێت و دروستکردنی لینکی تایبەت بە مۆبایل...") | |
| if os.path.exists(log_file): | |
| try: os.remove(log_file) | |
| except: pass | |
| # Launch tunnel with logfile | |
| tunnel_proc = subprocess.Popen( | |
| [cloudflared_path, "tunnel", "--url", "http://localhost:3000", "--logfile", log_file], | |
| cwd=base_dir | |
| ) | |
| # Wait for the URL to appear in log_file | |
| public_url = None | |
| for _ in range(30): | |
| time.sleep(1) | |
| if os.path.exists(log_file): | |
| try: | |
| with open(log_file, "r", encoding="utf-8", errors="ignore") as f: | |
| content = f.read() | |
| m = re.search(r'https://[a-zA-Z0-9\-]+\.trycloudflare\.com', content) | |
| if m: | |
| public_url = m.group(0) | |
| break | |
| except Exception: | |
| pass | |
| local_ip = get_local_ip() | |
| print("\n" + "=" * 65) | |
| print(" پیرۆزە! کۆمپیوتەرەکەت بووە سێرڤەر و دەتوانیت لە مۆبایلەوە بیکەیتەوە:") | |
| print("=" * 65) | |
| if public_url: | |
| print(f"\n📱 لینکی ڕاستەوخۆ بۆ مۆبایل (HTTPS - مایک و هەموو شتێک کاردەکات):") | |
| print(f" 👉 {public_url}\n") | |
| print(f"📶 لینکی ناوخۆیی وایفای (Wi-Fi Local):") | |
| print(f" 👉 http://{local_ip}:3000\n") | |
| print("=" * 65) | |
| print("تێبینی: ئەم پەنجەرەیە دامەخە تاوەکو مۆبایلەکەت پەیوەست بێت.") | |
| print("هەر کاتێک کارت تەواو بوو دەتوانیت دایبخەیت.") | |
| print("=" * 65) | |
| try: | |
| tunnel_proc.wait() | |
| except KeyboardInterrupt: | |
| tunnel_proc.terminate() | |
| if __name__ == "__main__": | |
| main() | |