-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackets.py
99 lines (77 loc) · 2.46 KB
/
packets.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
from json import loads
from random import randint
import socket
from SocketServer import UDPServer
from SocketServer import BaseRequestHandler
class BaseServer(UDPServer):
"""
This is where we add/change functionality to UDPServer.
"""
def next(self):
next = self.get_request()
return next
class User(object):
"""
This wraps BaseServer with an interface for humans.
Because we are all consenting adults,
you still have lots of power!
self.server gives you direct access to the BaseServer
subclass and all its interesting funcionality.
self.server.socket gives direct access to the socket
"""
def __init__(self, host='localhost', port=None):
if port == None:
port = randint(49152, 65535) # IANA ephemeral ports
ephemeral = True
else:
ephemeral = False
while True:
try:
self.server=BaseServer((host, port), BaseRequestHandler)
break
except socket.error, e:
if ephemeral and e.errno == 48:
port = randint(49152, 65535) # IANA ephemeral ports
else:
raise e
self.server.timeout = 0.1
self.server.allow_reuse_address = True
self.server.request_queue_size = 5
def sendto(self, addr, data):
self.server.socket.sendto(data, addr)
@property
def next(self):
return Packet(self.server.next(), self)
@property
def addr(self):
return self.server.socket.getsockname()
@property
def host(self):
return self.addr[0]
@property
def port(self):
return self.addr[1]
class Packet(object):
"""
UDP packet wrapper.
It's a wrapper that still gives you access to everything.
self.packet is the original package
and allows for simple replies with self.reply()
"""
def __init__(self, packet, user):
self.packet = packet
self.user = user
def reply(self, data):
self.user.sendto(self.addr, data)
@property
def body(self):
return self.packet[0][0].strip()
@property
def json(self):
return loads(self.body)
@property
def sock(self):
return self.packet[0][1]
@property
def addr(self):
return self.packet[1]