Best Python code snippet using lisa_python
UDPRedPitayaDiscovery.py
Source:UDPRedPitayaDiscovery.py  
1# -*- coding: utf-8 -*-2"""3Created on Thu Dec 17 17:54:31 20154@author: JD5"""6from __future__ import print_function7# Simple service discovery implementation based on UDP broadcast packets:8# This class sends a UDP broadcast packet on port 1952 and then listens for replies on port 1952+1.9# Those replies do not contain useful data, other than the sender's IP addresses10import socket11import select12import time13class UDPRedPitayaDiscovery():14    15    #def __init__(self, broadcast_address="255.255.255.255", port_number=1952):16    def __init__(self, broadcast_address="192.168.1.255", port_number=1952):17        self.port_number = port_number18        self.broadcast_address = broadcast_address19        self.sock_conn = None20        self.sock_server = None21        22        self.bVerbose = False23        24        self.startListening()25        26    def __del__(self):27        # print("UDPRedPitayaDiscovery::__del__()")28        pass29        30    def startListening(self):31        # Initialization:32        print('Creating client socket...')33        self.sock_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)34        self.sock_client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)35        self.connectClientSocket()36        37        print('Creating server socket...')38        HOST_LOCALHOST = ''       # means local host39        self.sock_server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)40        self.sock_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)41        self.sock_server.setblocking(0)42        self.sock_server.bind((HOST_LOCALHOST, self.port_number+1))43        # no need to call listen() since this is UDP44        45    def connectClientSocket(self):46        self.sock_client.connect((self.broadcast_address, self.port_number))47    def send_broadcast(self):48        # send a broadcast packet:49        self.sock_client.send(b"")50        pass51        52    def check_answers(self):53        # is there any data ?54        # note the [0] at the end which selects only the first output of the select()55        ready_to_read = select.select([self.sock_server], [], [], 0)[0]56        57        if ready_to_read:58            (mac_address_data, host_info) = self.sock_server.recvfrom(4096)59            mac_address_data = mac_address_data.decode('ascii') # received data is in bytes format and we handle the mac address as an ascii string internally60            # we don't care about the data, we are only looking for the IP addresses61            return (host_info[0], mac_address_data)62        else:63            return (None, None)64    def run_for_N_seconds(self, Timeout=1):65        if self.bVerbose:66            print('run()')67            68        # send a broadcast packet:69        self.send_broadcast()70        71        # then we check for answers:72        start_time = time.perf_counter()73        ElapsedTime = 074        while ElapsedTime < Timeout:75            76            (host, mac_address) = self.check_answers()77#            if not host is None:78            print((host, mac_address))79            80            time.sleep(0.1)81            ElapsedTime = time.perf_counter() - start_time82            83            84def main():85#    disc = UDPRedPitayaDiscovery("192.168.137.255")86    disc = UDPRedPitayaDiscovery("192.168.2.255")87    disc.run_for_N_seconds(10)88#    disc.send_broadcast()89#    time.sleep(0.1)90#    print(disc.check_answers())91#    while 1:92#        time.sleep(1)93#        print(disc.check_answers())94#    95    96if __name__ == "__main__":...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
