Repository navigation
Expand file tree
/
Copy pathpython_runner.py
More file actions
64 lines (53 loc) · 1.99 KB
/
Copy pathpython_runner.py
File metadata and controls
64 lines (53 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
"""
Python Learning Arena - Local Server & Launcher
میدان تمرین و بازبینی پایتون
"""
import os
import sys
import webbrowser
import socket
from http.server import HTTPServer, SimpleHTTPRequestHandler
if sys.platform == "win32":
try:
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
PORT = 8085
DIRECTORY = os.path.dirname(os.path.abspath(__file__))
def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('127.0.0.1', port)) == 0
class QuietHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def log_message(self, format, *args):
# Clean logging
sys.stdout.write(f"[{self.log_date_time_string()}] {args[0]} {args[1]}\n")
def run():
global PORT
while is_port_in_use(PORT):
PORT += 1
server_address = ('127.0.0.1', PORT)
httpd = HTTPServer(server_address, QuietHandler)
url = f"http://127.0.0.1:{PORT}/"
print("=" * 60)
print(" 🚀 PYTHON LEARNING ARENA (میدان تمرین و بازبینی پایتون)")
print(f" 📂 مسیر پروژه: {DIRECTORY}")
print(f" 🌐 آدرس وب: {url}")
print("=" * 60)
print(" در حال باز کردن مرورگر پیشفرض...")
print(" برای متوقف کردن سرور، کلیدهای Ctrl + C را فشار دهید.\n")
try:
webbrowser.open(url)
except Exception as e:
print(f"ناتوانی در باز کردن خودکار مرورگر: {e}")
print(f"لطفاً به صورت دستی این آدرس را باز کنید: {url}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n سرور با موفقیت متوقف شد.")
httpd.server_close()
if __name__ == '__main__':
run()