-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphost.py
More file actions
64 lines (57 loc) · 2.02 KB
/
phost.py
File metadata and controls
64 lines (57 loc) · 2.02 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
#!/data/data/com.termux/files/usr/bin/python3
import os, sys, subprocess
# --- Config ---
pidf = os.path.expanduser('~/py_host.pid')
host, port = '127.0.0.1', 8080
# ANSI color code
red = "\033[31m"
grn = "\033[32m"
yel = "\033[33m"
blu = "\033[34m"
mag = "\033[35m"
rst = "\033[0m" # for reset
b = "\033[1m" # b for bold
def banner():
print("\n\t\033[1:31m⟨ Always Kill The Server After Your Done ⟩")
print(f"\n{red}{b}\t\t┌──── Python Host ────┐")
print(f"\t\t| Kill:python3 {b}{mag}{sys.argv[0]} kill{red}|")
print(f"\t\t| Exit:{b}{mag}Ctrl+z{rst}{red} |")
print(f"\t\t└──────────────────────────┘{rst}")
banner()
# --- Functions ---
def k():
if os.path.exists(pidf):
try: os.kill(int(open(pidf).read()),9)
except: pass
os.remove(pidf)
print("\033[1;31mServer killed\033[0m")
else: print("\033[33mNo server running\033[0m")
# --- Kill handler ---
if len(sys.argv)>1 and sys.argv[1]=="kill": k(); sys.exit()
# --- Check running ---
if os.path.exists(pidf):
pid=int(open(pidf).read())
try: os.kill(pid,0)
except: os.remove(pidf)
else:
c=input("Server running. k=kill,n=exit: ")
if c=="k": k(); sys.exit()
else: sys.exit()
# --- File input ---
f=''
while True:
f=input("File (html/htm): ")
if f.endswith(('.html','.htm')) and os.path.isfile(f): break
print("\033[31mInvalid/not found\033[0m")
# --- Host prompt ---
if input("Host now? y/n: ").lower()=='y':
p=subprocess.Popen([sys.executable,'-m','http.server',str(port),'--bind',host],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
open(pidf,'w').write(str(p.pid))
url = f"http://{host}:{port}/{f}"
print(f"\033[32mServer: {url}\033[0m")
subprocess.run(['am','start','-a','android.intent.action.VIEW','-d',url])
print("\033[33mBackground live. Ctrl+C exits script\033[0m")
print(banner)
else:
print("\033[33mExit without hosting\033[0m")