def testClientToServerConnect(self): """Test the client receive of a server broadcast. """ import sys import nose if sys.platform.startswith("linux"): raise nose.SkipTest("Skipping this test on linux, I need to do it differently.") common_port = autoconnect.get_free_port() test_information = "Hello! This is some information. Connect on URL http://www.example.com:80/" # b = autoconnect.broadcaster.UdpBroadcaster(broadcast_address='127.0.0.1', broadcast_period=1) b = autoconnect.broadcaster.UdpBroadcaster(broadcast_period=1) b.start(test_information, [common_port]) r = autoconnect.receiver.UdpReceiver() # data, address = r.receive(common_port, interface='127.0.0.1', timeout="20") data, address = r.receive(common_port, timeout="20") b.stop() self.assertEquals(data, test_information)
def testRandomFreePort(self): """Test the get_random_port() and its checking of the free port. """ # Test I get an exception when there are no more free ports. retries = 1 port = 12123 # bind to the port we're going to test against: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(('', port)) self.open_ports.append(s) self.assertRaises(autoconnect.FreePortError, autoconnect.get_free_port, testing=(retries, port)) # Now try again this time letting it retry more then once. retries = 2 port = 0 # allows it to use random ports. free_port = autoconnect.get_free_port(testing=(retries, port)) # Try to bind to the free port after getting it: s2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s2.bind(('', free_port)) self.open_ports.append(s2)