forked from iBaa/PlexConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlexConnect.py
executable file
·111 lines (83 loc) · 3.04 KB
/
PlexConnect.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
"""
PlexConnect
Sources:
inter-process-communication (queue): http://pymotw.com/2/multiprocessing/communication.html
"""
import sys, time
from os import sep
import socket
from multiprocessing import Process, Pipe
import signal, errno
import DNSServer, WebServer
import Settings
from Debug import * # dprint()
def getIP_self():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('1.2.3.4', 1000))
IP = s.getsockname()[0]
dprint('PlexConnect', 0, "IP_self: "+IP)
return IP
g_shutdown = False
def sighandler_shutdown(signum, frame):
dprint('PlexConnect', 0, "Shutting down.")
global g_shutdown
g_shutdown = True
if pipe_DNSServer != None:
pipe_DNSServer[0].send('shutdown')
if pipe_WebServer != None:
pipe_WebServer[0].send('shutdown')
if __name__=="__main__":
signal.signal(signal.SIGINT, sighandler_shutdown)
signal.signal(signal.SIGTERM, sighandler_shutdown)
pipe_DNSServer = None
pipe_WebServer = None
param = {}
param['LogFile'] = sys.path[0] + sep + 'PlexConnect.log'
dinit('PlexConnect', param, True) # init logging, new file, main process
dprint('PlexConnect', 0, "***")
dprint('PlexConnect', 0, "PlexConnect")
dprint('PlexConnect', 0, "Press CTRL-C to shut down.")
dprint('PlexConnect', 0, "***")
# Settings
cfg = Settings.CSettings()
param['CSettings'] = cfg
param['IP_self'] = getIP_self()
param['HostToIntercept'] = 'trailers.apple.com'
# Logfile, re-init
param['LogLevel'] = cfg.getSetting('loglevel')
dinit('PlexConnect', param) # re-init logfile with loglevel
# init DNSServer
if cfg.getSetting('enable_dnsserver')=='True':
pipe_DNSServer = Pipe() # endpoint [0]-PlexConnect, [1]-DNSServer
p_DNSServer = Process(target=DNSServer.Run, args=(pipe_DNSServer[1], param))
p_DNSServer.start()
time.sleep(0.1)
if not p_DNSServer.is_alive():
dprint('PlexConnect', 0, "DNSServer not alive. Shutting down.")
sys.exit(1)
# init WebServer
pipe_WebServer = Pipe() # endpoint [0]-PlexConnect, [1]-WebServer
p_WebServer = Process(target=WebServer.Run, args=(pipe_WebServer[1], param))
p_WebServer.start()
time.sleep(0.1)
if not p_WebServer.is_alive():
dprint('PlexConnect', 0, "WebServer not alive. Shutting down.")
if cfg.getSetting('enable_dnsserver')=='True':
pipe_DNSServer[0].send('shutdown')
p_DNSServer.join()
sys.exit(1)
# work until shutdown
# ...or just wait until child processes are done
while g_shutdown==False:
# do something important
try:
time.sleep(60)
except IOError as e:
if e.errno == errno.EINTR and g_shutdown == True:
pass # mask "IOError: [Errno 4] Interrupted function call"
else:
raise
if cfg.getSetting('enable_dnsserver')=='True':
p_DNSServer.join()
p_WebServer.join()