-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp_pipe.py
More file actions
47 lines (34 loc) · 1.23 KB
/
udp_pipe.py
File metadata and controls
47 lines (34 loc) · 1.23 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
import socket
import socketserver
class MyUDPHandler(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()
in_socket = self.request[1]
# print("{} wrote:".format(self.client_address[0]))
# print(data)
# Send out data to all output ports
UDP_IP = "127.0.0.1"
out_ports = [5311, 5411]
for port in out_ports:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# print("port:", port)
sock.sendto(data, (UDP_IP, port))
# socket.sendto(data.upper(), self.client_address)
def main():
UDP_IP = "127.0.0.1"
out_ports = [5311, 5411]
MESSAGE = "Hello, World!"
print("UDP target IP:", UDP_IP)
print("UDP target port:", out_ports)
print("message:", MESSAGE)
HOST, in_port = "localhost", 5011
while True:
with socketserver.UDPServer((HOST, in_port), MyUDPHandler) as server:
server.serve_forever()
main()