Python errno.EADDRINUSE Examples
The following are 30
code examples of errno.EADDRINUSE().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
errno
, or try the search function
.
Example #1
Source File: test_events.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_create_server_addr_in_use(self): sock_ob = socket.socket(type=socket.SOCK_STREAM) sock_ob.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock_ob.bind(('0.0.0.0', 0)) f = self.loop.create_server(MyProto, sock=sock_ob) server = self.loop.run_until_complete(f) sock = server.sockets[0] host, port = sock.getsockname() f = self.loop.create_server(MyProto, host=host, port=port) with self.assertRaises(OSError) as cm: self.loop.run_until_complete(f) self.assertEqual(cm.exception.errno, errno.EADDRINUSE) server.close()
Example #2
Source File: test_llcp_llc.py From nfcpy with European Union Public License 1.1 | 6 votes |
def test_bind_by_addr(self, llc, raw, ldl, dlc): llc.bind(raw, 16) assert llc.getsockname(raw) == 16 for i, sock in enumerate([ldl, dlc]): with pytest.raises(nfc.llcp.Error) as excinfo: llc.bind(sock, 16) assert excinfo.value.errno == errno.EACCES llc.bind(ldl, 63) assert llc.getsockname(ldl) == 63 with pytest.raises(nfc.llcp.Error) as excinfo: llc.bind(dlc, 63) assert excinfo.value.errno == errno.EADDRINUSE with pytest.raises(nfc.llcp.Error) as excinfo: llc.bind(dlc, 64) assert excinfo.value.errno == errno.EFAULT with pytest.raises(nfc.llcp.Error) as excinfo: llc.bind(dlc, -1) assert excinfo.value.errno == errno.EFAULT llc.bind(dlc, 62) assert llc.getsockname(dlc) == 62
Example #3
Source File: llc.py From nfcpy with European Union Public License 1.1 | 6 votes |
def _bind_by_name(self, socket, name): if not service_name_format.match(name): raise err.Error(errno.EFAULT) with self.lock: if self.snl.get(name) is not None: raise err.Error(errno.EADDRINUSE) addr = wks_map.get(name) if addr is None: try: addr = 16 + self.sap[16:32].index(None) except ValueError: raise err.Error(errno.EADDRNOTAVAIL) socket.bind(addr) self.sap[addr] = ServiceAccessPoint(addr, self) self.sap[addr].insert_socket(socket) self.snl[name] = addr
Example #4
Source File: main.py From edgedb with Apache License 2.0 | 6 votes |
def find_available_port(port_range=(49152, 65535), max_tries=1000): low, high = port_range port = low try_no = 0 while try_no < max_tries: try_no += 1 port = random.randint(low, high) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.bind(('localhost', port)) except socket.error as e: if e.errno == errno.EADDRINUSE: continue raise finally: sock.close() break else: port = None return port
Example #5
Source File: eventlet_backdoor.py From oslo.service with Apache License 2.0 | 6 votes |
def _try_open_unix_domain_socket(socket_path): try: return eventlet.listen(socket_path, socket.AF_UNIX) except socket.error as e: if e.errno != errno.EADDRINUSE: # NOTE(harlowja): Some other non-address in use error # occurred, since we aren't handling those, re-raise # and give up... raise else: # Attempt to remove the file before opening it again. try: os.unlink(socket_path) except OSError as e: if e.errno != errno.ENOENT: # NOTE(harlowja): File existed, but we couldn't # delete it, give up... raise return eventlet.listen(socket_path, socket.AF_UNIX)
Example #6
Source File: test_socket.py From oss-ftp with MIT License | 6 votes |
def test_getsockaddrarg(self): sock = socket.socket() self.addCleanup(sock.close) port = test_support.find_unused_port() big_port = port + 65536 neg_port = port - 65536 self.assertRaises(OverflowError, sock.bind, (HOST, big_port)) self.assertRaises(OverflowError, sock.bind, (HOST, neg_port)) # Since find_unused_port() is inherently subject to race conditions, we # call it a couple times if necessary. for i in itertools.count(): port = test_support.find_unused_port() try: sock.bind((HOST, port)) except OSError as e: if e.errno != errno.EADDRINUSE or i == 5: raise else: break
Example #7
Source File: discovery.py From networkzero with MIT License | 6 votes |
def _bind_with_timeout(bind_function, args, n_tries=3, retry_interval_s=0.5): """Attempt to bind a socket a number of times with a short interval in between Especially on Linux, crashing out of a networkzero process can leave the sockets lingering and unable to re-bind on startup. We give it a few goes here to see if we can bind within a couple of seconds. """ n_tries_left = n_tries while n_tries_left > 0: try: return bind_function(*args) except zmq.error.ZMQError as exc: _logger.warn("%s; %d tries remaining", exc, n_tries_left) n_tries_left -= 1 except OSError as exc: if exc.errno == errno.EADDRINUSE: _logger.warn("%s; %d tries remaining", exc, n_tries_left) n_tries_left -= 1 else: raise else: raise core.SocketAlreadyExistsError("Failed to bind after %s tries" % n_tries)
Example #8
Source File: test_socket.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_getsockaddrarg(self): sock = socket.socket() self.addCleanup(sock.close) port = support.find_unused_port() big_port = port + 65536 neg_port = port - 65536 self.assertRaises(OverflowError, sock.bind, (HOST, big_port)) self.assertRaises(OverflowError, sock.bind, (HOST, neg_port)) # Since find_unused_port() is inherently subject to race conditions, we # call it a couple times if necessary. for i in itertools.count(): port = support.find_unused_port() try: sock.bind((HOST, port)) except OSError as e: if e.errno != errno.EADDRINUSE or i == 5: raise else: break
Example #9
Source File: utils_vsock.py From avocado-vt with GNU General Public License v2.0 | 6 votes |
def get_guest_cid(guest_cid): """ Get an unused guest cid from system :param guest_cid: Requested guest cid :return Available guest cid """ vsock_fd = os.open(VSOCK_PATH, os.O_RDWR) try: while guest_cid: cid_c = struct.pack('L', guest_cid) try: fcntl.ioctl( vsock_fd, arch.VHOST_VSOCK_SET_GUEST_CID, cid_c) except IOError as e: if e.errno == errno.EADDRINUSE: guest_cid += 1 continue else: raise e else: return guest_cid finally: os.close(vsock_fd)
Example #10
Source File: test_llcp_llc.py From nfcpy with European Union Public License 1.1 | 6 votes |
def test_bind_by_name(self, llc, raw, ldl, dlc): with pytest.raises(nfc.llcp.Error) as excinfo: llc.bind(dlc, 'urn:nfc:snep') assert excinfo.value.errno == errno.EFAULT llc.bind(dlc, 'urn:nfc:sn:snep') assert llc.getsockname(dlc) == 4 with pytest.raises(nfc.llcp.Error) as excinfo: llc.bind(ldl, 'urn:nfc:sn:snep') assert excinfo.value.errno == errno.EADDRINUSE llc.bind(ldl, 'urn:nfc:xsn:nfcpy.org:service') assert llc.getsockname(ldl) == 16 for sap in range(17, 32): sock = llc.socket(nfc.llcp.llc.RAW_ACCESS_POINT) llc.bind(sock, 'urn:nfc:sn:use_sap-{}'.format(sap)) assert llc.getsockname(sock) == sap with pytest.raises(nfc.llcp.Error) as excinfo: llc.bind(raw, 'urn:nfc:sn:sap-32') assert excinfo.value.errno == errno.EADDRNOTAVAIL
Example #11
Source File: test_socket.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_getsockaddrarg(self): sock = socket.socket() self.addCleanup(sock.close) port = test_support.find_unused_port() big_port = port + 65536 neg_port = port - 65536 self.assertRaises(OverflowError, sock.bind, (HOST, big_port)) self.assertRaises(OverflowError, sock.bind, (HOST, neg_port)) # Since find_unused_port() is inherently subject to race conditions, we # call it a couple times if necessary. for i in itertools.count(): port = test_support.find_unused_port() try: sock.bind((HOST, port)) except OSError as e: if e.errno != errno.EADDRINUSE or i == 5: raise else: break
Example #12
Source File: snap.py From pypot with GNU General Public License v3.0 | 6 votes |
def run(self, quiet=None, server=''): """ Start the tornado server, run forever. 'quiet' and 'server' arguments are no longer used, they are keep only for backward compatibility """ try: loop = IOLoop() http_server = HTTPServer(WSGIContainer(self.app)) http_server.listen(self.port) loop.start() except socket.error as serr: # Re raise the socket error if not "[Errno 98] Address already in use" if serr.errno != errno.EADDRINUSE: raise serr else: logger.warning("""The webserver port {} is already used. The SnapRobotServer is maybe already run or another software use this port.""".format(self.port))
Example #13
Source File: utils.py From kitty with GNU General Public License v3.0 | 6 votes |
def __call__(self, group_id: Optional[str] = None) -> bool: import socket name = '{}-ipc-{}'.format(appname, os.geteuid()) if group_id: name += '-{}'.format(group_id) s = socket.socket(family=socket.AF_UNIX) # First try with abstract UDS addr = '\0' + name try: s.bind(addr) except OSError as err: if err.errno == errno.ENOENT: return single_instance_unix(name) if err.errno == errno.EADDRINUSE: s.connect(addr) self.socket = s return False raise s.listen() self.socket = s # prevent garbage collection from closing the socket s.set_inheritable(False) atexit.register(remove_socket_file, s) return True
Example #14
Source File: test_events.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_create_server_addr_in_use(self): sock_ob = socket.socket(type=socket.SOCK_STREAM) sock_ob.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock_ob.bind(('0.0.0.0', 0)) f = self.loop.create_server(MyProto, sock=sock_ob) server = self.loop.run_until_complete(f) sock = server.sockets[0] host, port = sock.getsockname() f = self.loop.create_server(MyProto, host=host, port=port) with self.assertRaises(OSError) as cm: self.loop.run_until_complete(f) self.assertEqual(cm.exception.errno, errno.EADDRINUSE) server.close()
Example #15
Source File: test_events.py From annotated-py-projects with MIT License | 6 votes |
def test_create_server_addr_in_use(self): sock_ob = socket.socket(type=socket.SOCK_STREAM) sock_ob.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock_ob.bind(('0.0.0.0', 0)) f = self.loop.create_server(MyProto, sock=sock_ob) server = self.loop.run_until_complete(f) sock = server.sockets[0] host, port = sock.getsockname() f = self.loop.create_server(MyProto, host=host, port=port) with self.assertRaises(OSError) as cm: self.loop.run_until_complete(f) self.assertEqual(cm.exception.errno, errno.EADDRINUSE) server.close()
Example #16
Source File: core.py From grizzly with Mozilla Public License 2.0 | 6 votes |
def _create_listening_socket(allow_remote, requested_port, retries=20): # The intention of this function is to contain the socket creation code # along with all the searching and retrying code. If a specific port is requested # and it is not available a socket.error will be raised. for retry in reversed(range(retries)): sock = None try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.settimeout(0.25) # find an unused port and avoid blocked ports # see: dxr.mozilla.org/mozilla-central/source/netwerk/base/nsIOService.cpp port = random.randint(0x2000, 0xFFFF) if requested_port is None else requested_port sock.bind(("0.0.0.0" if allow_remote else "127.0.0.1", port)) sock.listen(5) except (OSError, socket.error) as soc_e: if sock is not None: sock.close() if retry > 1 and soc_e.errno in (errno.EADDRINUSE, 10013): time.sleep(0.1) continue raise break return sock
Example #17
Source File: clai_run.py From clai with MIT License | 6 votes |
def is_port_busy(host, port, reconnect): socket_to_check = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: socket_to_check.bind((host, port)) except socket.error as exception: if exception.errno == errno.EADDRINUSE: if not reconnect: print("Port is already in use") return True print(f'something else raised in the socket: {exception}') finally: socket_to_check.close() return False
Example #18
Source File: test_socket.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_getsockaddrarg(self): sock = socket.socket() self.addCleanup(sock.close) port = support.find_unused_port() big_port = port + 65536 neg_port = port - 65536 self.assertRaises(OverflowError, sock.bind, (HOST, big_port)) self.assertRaises(OverflowError, sock.bind, (HOST, neg_port)) # Since find_unused_port() is inherently subject to race conditions, we # call it a couple times if necessary. for i in itertools.count(): port = support.find_unused_port() try: sock.bind((HOST, port)) except OSError as e: if e.errno != errno.EADDRINUSE or i == 5: raise else: break
Example #19
Source File: cluster.py From edgedb with Apache License 2.0 | 6 votes |
def find_available_port(port_range=(49152, 65535), max_tries=1000): low, high = port_range port = low try_no = 0 while try_no < max_tries: try_no += 1 port = random.randint(low, high) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.bind(('localhost', port)) except socket.error as e: if e.errno == errno.EADDRINUSE: continue finally: sock.close() break else: port = None return port
Example #20
Source File: utils.py From kitty with GNU General Public License v3.0 | 5 votes |
def single_instance_unix(name: str) -> bool: import socket for path in unix_socket_paths(name): socket_path = path.rpartition('.')[0] + '.sock' fd = os.open(path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC | os.O_CLOEXEC) try: fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) except OSError as err: if err.errno in (errno.EAGAIN, errno.EACCES): # Client s = socket.socket(family=socket.AF_UNIX) s.connect(socket_path) single_instance.socket = s return False raise s = socket.socket(family=socket.AF_UNIX) try: s.bind(socket_path) except OSError as err: if err.errno in (errno.EADDRINUSE, errno.EEXIST): os.unlink(socket_path) s.bind(socket_path) else: raise single_instance.socket = s # prevent garbage collection from closing the socket atexit.register(remove_socket_file, s, socket_path) s.listen() s.set_inheritable(False) return True return False
Example #21
Source File: _test_multiprocessing.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_rapid_restart(self): authkey = os.urandom(32) manager = QueueManager( address=(test.support.HOST, 0), authkey=authkey, serializer=SERIALIZER) srvr = manager.get_server() addr = srvr.address # Close the connection.Listener socket which gets opened as a part # of manager.get_server(). It's not needed for the test. srvr.listener.close() manager.start() p = self.Process(target=self._putter, args=(manager.address, authkey)) p.daemon = True p.start() queue = manager.get_queue() self.assertEqual(queue.get(), 'hello world') del queue manager.shutdown() manager = QueueManager( address=addr, authkey=authkey, serializer=SERIALIZER) try: manager.start() except OSError as e: if e.errno != errno.EADDRINUSE: raise # Retry after some time, in case the old socket was lingering # (sporadic failure on buildbots) time.sleep(1.0) manager = QueueManager( address=addr, authkey=authkey, serializer=SERIALIZER) manager.shutdown() # # #
Example #22
Source File: unix_events.py From annotated-py-projects with MIT License | 5 votes |
def create_unix_server(self, protocol_factory, path=None, *, sock=None, backlog=100, ssl=None): if isinstance(ssl, bool): raise TypeError('ssl argument must be an SSLContext or None') if path is not None: if sock is not None: raise ValueError( 'path and sock can not be specified at the same time') sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: sock.bind(path) except OSError as exc: sock.close() if exc.errno == errno.EADDRINUSE: # Let's improve the error message by adding # with what exact address it occurs. msg = 'Address {!r} is already in use'.format(path) raise OSError(errno.EADDRINUSE, msg) from None else: raise except: sock.close() raise else: if sock is None: raise ValueError( 'path was not specified, and no sock specified') if sock.family != socket.AF_UNIX: raise ValueError( 'A UNIX Domain Socket was expected, got {!r}'.format(sock)) server = base_events.Server(self, [sock]) sock.listen(backlog) sock.setblocking(False) self._start_serving(protocol_factory, sock, ssl, server) return server
Example #23
Source File: oscAPI.py From Pyonic-interpreter with GNU General Public License v3.0 | 5 votes |
def run(self): self.haveSocket = False # create socket self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # fix trouble if python leave without cleaning well the socket # not needed under windows, he can reuse addr even if the socket # are in fin2 or wait state. if os.name in ['posix', 'mac'] and hasattr(socket, 'SO_REUSEADDR'): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # try to bind the socket, retry if necessary while not self.haveSocket and self.isRunning: try : self.socket.bind((self.ipAddr, self.port)) self.socket.settimeout(0.5) self.haveSocket = True except socket.error as e: error, message = e.args # special handle for EADDRINUSE if error == errno.EADDRINUSE: Logger.error('OSC: Address %s:%i already in use, retry in 2 second' % (self.ipAddr, self.port)) else: self.haveSocket = False # sleep 2 second before retry time.sleep(2) Logger.info('OSC: listening for Tuio on %s:%i' % (self.ipAddr, self.port)) while self.isRunning: try: message = self.socket.recv(65535) self._queue_message(message) except Exception as e: if type(e) == socket.timeout: continue Logger.exception('OSC: Error in Tuio recv()') return 'no data arrived'
Example #24
Source File: server.py From aioftp with Apache License 2.0 | 5 votes |
def _start_passive_server(self, connection, handler_callback): if self.available_data_ports is not None: viewed_ports = set() while True: try: priority, port = self.available_data_ports.get_nowait() if port in viewed_ports: raise errors.NoAvailablePort viewed_ports.add(port) passive_server = await asyncio.start_server( handler_callback, connection.server_host, port, ssl=self.ssl, **self._start_server_extra_arguments, ) connection.passive_server_port = port break except asyncio.QueueEmpty: raise errors.NoAvailablePort except OSError as err: self.available_data_ports.put_nowait((priority + 1, port)) if err.errno != errno.EADDRINUSE: raise else: passive_server = await asyncio.start_server( handler_callback, connection.server_host, connection.passive_server_port, ssl=self.ssl, **self._start_server_extra_arguments, ) return passive_server
Example #25
Source File: unix_events.py From ironpython3 with Apache License 2.0 | 5 votes |
def create_unix_server(self, protocol_factory, path=None, *, sock=None, backlog=100, ssl=None): if isinstance(ssl, bool): raise TypeError('ssl argument must be an SSLContext or None') if path is not None: if sock is not None: raise ValueError( 'path and sock can not be specified at the same time') sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: sock.bind(path) except OSError as exc: sock.close() if exc.errno == errno.EADDRINUSE: # Let's improve the error message by adding # with what exact address it occurs. msg = 'Address {!r} is already in use'.format(path) raise OSError(errno.EADDRINUSE, msg) from None else: raise except: sock.close() raise else: if sock is None: raise ValueError( 'path was not specified, and no sock specified') if sock.family != socket.AF_UNIX: raise ValueError( 'A UNIX Domain Socket was expected, got {!r}'.format(sock)) server = base_events.Server(self, [sock]) sock.listen(backlog) sock.setblocking(False) self._start_serving(protocol_factory, sock, ssl, server) return server
Example #26
Source File: test_events.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_create_server_dual_stack(self): f_proto = asyncio.Future(loop=self.loop) class TestMyProto(MyProto): def connection_made(self, transport): super().connection_made(transport) f_proto.set_result(self) try_count = 0 while True: try: port = support.find_unused_port() f = self.loop.create_server(TestMyProto, host=None, port=port) server = self.loop.run_until_complete(f) except OSError as ex: if ex.errno == errno.EADDRINUSE: try_count += 1 self.assertGreaterEqual(5, try_count) continue else: raise else: break client = socket.socket() client.connect(('127.0.0.1', port)) client.send(b'xxx') proto = self.loop.run_until_complete(f_proto) proto.transport.close() client.close() f_proto = asyncio.Future(loop=self.loop) client = socket.socket(socket.AF_INET6) client.connect(('::1', port)) client.send(b'xxx') proto = self.loop.run_until_complete(f_proto) proto.transport.close() client.close() server.close()
Example #27
Source File: test_smtplib.py From ironpython3 with Apache License 2.0 | 5 votes |
def testSourceAddress(self): # connect port = support.find_unused_port() try: smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3, source_address=('127.0.0.1', port)) self.assertEqual(smtp.source_address, ('127.0.0.1', port)) self.assertEqual(smtp.local_hostname, 'localhost') smtp.quit() except OSError as e: if e.errno == errno.EADDRINUSE: self.skipTest("couldn't bind to port %d" % port) raise
Example #28
Source File: test_ftplib.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_source_address_passive_connection(self): port = support.find_unused_port() self.client.source_address = (HOST, port) try: with self.client.transfercmd('list') as sock: self.assertEqual(sock.getsockname()[1], port) except OSError as e: if e.errno == errno.EADDRINUSE: self.skipTest("couldn't bind to port %d" % port) raise
Example #29
Source File: test_ftplib.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_source_address(self): self.client.quit() port = support.find_unused_port() try: self.client.connect(self.server.host, self.server.port, source_address=(HOST, port)) self.assertEqual(self.client.sock.getsockname()[1], port) self.client.quit() except OSError as e: if e.errno == errno.EADDRINUSE: self.skipTest("couldn't bind to port %d" % port) raise
Example #30
Source File: test_events.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_create_server_dual_stack(self): f_proto = asyncio.Future(loop=self.loop) class TestMyProto(MyProto): def connection_made(self, transport): super().connection_made(transport) f_proto.set_result(self) try_count = 0 while True: try: port = support.find_unused_port() f = self.loop.create_server(TestMyProto, host=None, port=port) server = self.loop.run_until_complete(f) except OSError as ex: if ex.errno == errno.EADDRINUSE: try_count += 1 self.assertGreaterEqual(5, try_count) continue else: raise else: break client = socket.socket() client.connect(('127.0.0.1', port)) client.send(b'xxx') proto = self.loop.run_until_complete(f_proto) proto.transport.close() client.close() f_proto = asyncio.Future(loop=self.loop) client = socket.socket(socket.AF_INET6) client.connect(('::1', port)) client.send(b'xxx') proto = self.loop.run_until_complete(f_proto) proto.transport.close() client.close() server.close()