-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPkit.py
132 lines (86 loc) · 4.99 KB
/
UDPkit.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import socket
print "IN uuu"
BUFFERSIZE = 4096 # buffer size for recvfrom
class FromSocket(object):
"""returns instantiated "receivefrom" socket"""
def __init__(self , thisIPaddr = "127.0.0.1" , thisPort = 50001 ):
"""
Create inbound socket
"""
try:
self.thisIPAddr, self.thisPort = thisIPaddr , thisPort
self.thisAddr = (self.thisIPAddr, self.thisPort)
#super(ToSocket,self).__init__() # initialize next higher superclass
self.from_sock = socket.socket( socket.AF_INET , socket.SOCK_DGRAM )
self.from_sock.settimeout(2.0) # non-zero timeout used to catch exceptions
print self.from_sock._sock
print self.thisAddr
self.from_sock.bind(self.thisAddr)
except Exception as exeptn:
print type(exeptn),exeptn
self.from_sock.close() # explicitly close socket on failure
raise exeptn # reraise exception
finally: print "init for FromSocket done"
def recv(self):
while True: # input loop, also used to retry
try:
self.thatPayload, self.thatAddr = self.from_sock.recvfrom( BUFFERSIZE )
self.thatIPaddr , self.thatPort = self.thatAddr
print self.thatAddr
return self.thatPayload
except socket.timeout: continue # retry after timeout
except Exception as exeptn: # end loop on any exception
self.from_sock.close() # explicitly close socket
raise exeptn # then reraise exception
#if TRACING: print self.ShowDataGram("inbound") # Display IP and UDP formats
finally: print "recv done"
def close(self): self.from_sock.close() # explicitly close socket
class ToSocket(object):
""" Create instance of outbound UDP socket
The constructor determines a destination address for all packets produced
by sendto(). It is the tuple (thatIPaddr, thatPort).
When recvfrom returns it provides the Payload and the sending
address tuple (thisIPaddr, thisPort). It gets these from
the packet it receives.
The sendto call to the socket could block.
This implementation uses timeout to ensure that a blocking write
will eventually receive a timeout and any pending exogenous events.
This permits signals such as ctl-c to terminate the sendto call and
the process.
If the operator uses the attention key (ctl-c) or the task terminates,
the state of the underlying operating system's IPstack state
is reset.
"""
def __init__(self , thatIPaddr = "127.0.0.1" , thatPort = 50001 ):
"""UDPsocket.__init__(
thatIPaddr = "127.0.0.1" # ipv4 address to send to
thatPort = 50001 # port to send to
"""
try:
self.thatIPaddr , self.thatPort = thatIPaddr , thatPort
self.thatAddr = (self.thatIPaddr, self.thatPort)
print self.thatAddr," in ToSocket"
#super(ToSocket,self).__init__(self.thatAddr) # initialize next higher superclass
self.to_sock = socket.socket( socket.AF_INET , socket.SOCK_DGRAM )
self.to_sock.settimeout(2.0) # non-zero timeout is used to catch exceptions
except Exception as exeptn:
print type(exeptn),exeptn
self.to_sock.close() # explicitly close socket
raise exeptn # reraise exception
finally:
print "init for ToSocket done"
def send(self,thisPayload):
""" sends thisPayload to self.thatAddr
"""
while True:
self.thisPayload = thisPayload
try:
return self.to_sock.sendto(self.thisPayload, self.thatAddr)
# returns number of bytes in thisPayload sent to thatAddr
except socket.timeout: continue # retry after timeout
except Exception as exeptn:
self.to_sock.close() # explicitly close socket
raise exeptn # reraise exception
#if TRACING: print self.ShowDataGram("outbound") # Display IP, UDP and Payload
def close(self):
self.to_sock.close()