-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten.py
36 lines (29 loc) · 994 Bytes
/
listen.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
import socket
def listen_for_udp_data(ip, port, timeout=5):
# Create a UDP socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the specified IP and port
sock.bind((ip, port))
# Set a timeout for the socket
sock.settimeout(timeout)
print(f"Listening for UDP data on {ip}:{port}")
try:
# Receive data
data, addr = sock.recvfrom(1024)
print(f"Received data from {addr}")
print(f"Data: {data.decode()}")
return True
except socket.timeout:
print("No data received within the timeout period")
return False
finally:
# Close the socket
sock.close()
# Example usage
ip_to_listen = "0.0.0.0" # listen on 0.0.0.0 to receive data from any IP
port_to_listen = 8000
data_received = listen_for_udp_data(ip_to_listen, port_to_listen)
if data_received:
print("Data was received successfully")
else:
print("No data was received")