-
Notifications
You must be signed in to change notification settings - Fork 14
/
Listener.py
112 lines (101 loc) · 3.42 KB
/
Listener.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
112
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
##############################################
# Home : http://netkiller.github.io
# Author: Neo <netkiller@msn.com>
##############################################
try:
from struct import *
from optparse import OptionParser, OptionGroup
from Protocol import *
import socketserver, os, sys
except ImportError as err:
print("Error: %s" %(err))
class TMSHandler(socketserver.BaseRequestHandler):
"""
This class works similar to the TCP handler class, except that
self.request consists of a pair of data and client socket, and since
there is no connection the client address must be given explicitly
when sending data back via sendto().
"""
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print("{} wrote:".format(self.client_address[0]))
print(data)
#.decode(encoding='UTF-8')
#socket.sendto(data.upper(), self.client_address)
#socket.sendto(data, self.client_address)
socket.sendto(data, self.client_address)
print(self.client_address)
class LRRPHandler(socketserver.BaseRequestHandler):
"""
This class works similar to the TCP handler class, except that
self.request consists of a pair of data and client socket, and since
there is no connection the client address must be given explicitly
when sending data back via sendto().
"""
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print("{} wrote:".format(self.client_address[0]))
print(data)
#.decode(encoding='UTF-8')
#socket.sendto(data.upper(), self.client_address)
socket.sendto(data, self.client_address)
class ARSHandler(socketserver.BaseRequestHandler):
"""
This class works similar to the TCP handler class, except that
self.request consists of a pair of data and client socket, and since
there is no connection the client address must be given explicitly
when sending data back via sendto().
"""
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print("{} wrote:".format(self.client_address[0]))
print(data)
#.decode(encoding='UTF-8')
#socket.sendto(data.upper(), self.client_address)
socket.sendto(data, self.client_address)
def daemon(ip, port, headler):
pid = os.fork()
print(pid)
if pid > 0:
sys.exit(0)
server = socketserver.UDPServer((ip, port), headler)
server.serve_forever()
pass
if __name__ == "__main__":
try:
#if options.daemon:
pid = os.fork()
if pid == 0:
print('Starting TMS Listener...')
print("Child Process: PID# %s" % os.getpid() )
HOST, PORT = "192.168.6.2", 4007
tms = socketserver.UDPServer((HOST, PORT), TMSHandler)
#tms.daemon = True
tms.serve_forever()
else:
#sys.exit(0)
pid = os.fork()
if pid == 0:
print('Starting LRRP Listener...')
print("Child Process: PID# %s" % os.getpid() )
HOST, PORT = "192.168.6.2", 4001
lrrp = socketserver.UDPServer((HOST, PORT), LRRPHandler)
lrrp.serve_forever()
else:
#sys.exit(0)
pid = os.fork()
if pid == 0:
print('Starting ARS Listener...')
print("Child Process: PID# %s" % os.getpid() )
HOST, PORT = "192.168.6.2", 4005
ars = socketserver.UDPServer((HOST, PORT), ARSHandler)
ars.serve_forever()
else:
sys.exit(0)
except OSError as err:
print(err)