def testBroadcast(self): ss = socketutil.create_bc_socket((None, 0)) port = ss.getsockname()[1] cs = socketutil.create_bc_socket() for bcaddr in config.BROADCAST_ADDRS: try: cs.sendto(b"monkey", 0, (bcaddr, port)) except socket.error as x: err = getattr(x, "errno", x.args[0]) # handle some errno that some platforms like to throw if err not in socketutil.ERRNO_EADDRNOTAVAIL and err not in socketutil.ERRNO_EADDRINUSE: raise data, _ = ss.recvfrom(500) assert b"monkey" == data cs.close() ss.close()
def testCreateBoundSockets(self): s = socketutil.create_socket(bind=('127.0.0.1', 0)) assert socket.AF_INET == s.family bs = socketutil.create_bc_socket(bind=('127.0.0.1', 0)) assert '127.0.0.1' == s.getsockname()[0] assert '127.0.0.1' == bs.getsockname()[0] s.close() bs.close() with pytest.raises(ValueError): socketutil.create_socket(bind=('localhost', 12345), connect=('localhost', 1234))
def testCreateBoundSockets6(self): if not has_ipv6: pytest.skip("no ipv6 capability") s = socketutil.create_socket(bind=('::1', 0)) assert socket.AF_INET6 == s.family bs = socketutil.create_bc_socket(bind=('::1', 0)) assert ':' in s.getsockname()[0] assert ':' in bs.getsockname()[0] s.close() bs.close() with pytest.raises(ValueError): socketutil.create_socket(bind=('::1', 12345), connect=('::1', 1234))
def testCreateUnboundSockets(self): s = socketutil.create_socket() assert socket.AF_INET == s.family bs = socketutil.create_bc_socket() assert socket.AF_INET == bs.family with contextlib.suppress(socket.error): host, port = s.getsockname() # can either fail with socket.error or return (host,0) assert 0 == port with contextlib.suppress(socket.error): host, port = bs.getsockname() # can either fail with socket.error or return (host,0) assert 0 == port s.close() bs.close()
def testCreateUnboundSockets6(self): if not has_ipv6: pytest.skip("no ipv6 capability") s = socketutil.create_socket(ipv6=True) assert socket.AF_INET6 == s.family bs = socketutil.create_bc_socket(ipv6=True) assert socket.AF_INET6 == bs.family with contextlib.suppress(socket.error): host, port, _, _ = s.getsockname() # can either fail with socket.error or return (host,0) assert 0 == port with contextlib.suppress(socket.error): host, port, _, _ = bs.getsockname() # can either fail with socket.error or return (host,0) assert 0 == port s.close() bs.close()
def testCreateUnboundSockets(self): s = socketutil.create_socket() assert socket.AF_INET == s.family bs = socketutil.create_bc_socket() assert socket.AF_INET == bs.family try: host, port = s.getsockname() # can either fail with socket.error or return (host,0) assert 0 == port except socket.error: pass try: host, port = bs.getsockname() # can either fail with socket.error or return (host,0) assert 0 == port except socket.error: pass s.close() bs.close()