Get an open port on the machine.
517670
def get_open_port(host="localhost"):
    
    temp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    temp_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    temp_sock.bind((host, 0))
    port = temp_sock.getsockname()[1]
    temp_sock.close()
    del temp_sock
    return port
1162266
def get_free_udp_port():
    '''
    
    '''
    import socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 0))
    addr = sock.getsockname()
    sock.close()
    return addr[1]
852826
def open_socket(host, port) # :nodoc:
      return Timeout.timeout(@open_timeout, Net::OpenTimeout) {
        if defined? SOCKSSocket and ENV["SOCKS_SERVER"]
          @passive = true
          sock = SOCKSSocket.open(host, port)
        else
          sock = TCPSocket.open(host, port)
        end
        io = BufferedSocket.new(sock)
        io.read_timeout = @read_timeout
        io
      }
    end
1019658
def show_port(self, port, **_params):
        """"""
        return self.get(self.port_path % (port), params=_params)
324506
def open(self):
        
        self.device = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.device.connect((self.host, self.port))

        if self.device is None:
            print "Could not open socket for %s" % self.host
280806
def detect_port(port):
    ''''''
    socket_test = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
        socket_test.connect(('127.0.0.1', int(port)))
        socket_test.close()
        return True
    except:
        return False
228639
def Open(self):
        """"""
        # Make sure we close any previous handle open to this usb device.
        port_path = tuple(self.port_path)
        with self._HANDLE_CACHE_LOCK:
            old_handle = self._HANDLE_CACHE.get(port_path)
            if old_handle is not None:
                old_handle.Close()

        self._read_endpoint = None
        self._write_endpoint = None

        for endpoint in self._setting.iterEndpoints():
            address = endpoint.getAddress()
            if address & libusb1.USB_ENDPOINT_DIR_MASK:
                self._read_endpoint = address
                self._max_read_packet_len = endpoint.getMaxPacketSize()
            else:
                self._write_endpoint = address

        assert self._read_endpoint is not None
        assert self._write_endpoint is not None

        handle = self._device.open()
        iface_number = self._setting.getNumber()
        try:
            if (platform.system() != 'Windows'
                    and handle.kernelDriverActive(iface_number)):
                handle.detachKernelDriver(iface_number)
        except libusb1.USBError as e:
            if e.value == libusb1.LIBUSB_ERROR_NOT_FOUND:
                _LOG.warning('Kernel driver not found for interface: %s.', iface_number)
            else:
                raise
        handle.claimInterface(iface_number)
        self._handle = handle
        self._interface_number = iface_number

        with self._HANDLE_CACHE_LOCK:
            self._HANDLE_CACHE[port_path] = self
        # When this object is deleted, make sure it's closed.
        weakref.ref(self, self.Close)
15076
def connect(self):
        """"""
        transport = self._defs.find_device()
        if not transport:
            raise interface.NotFoundError('{} not connected'.format(self))

        log.debug('using transport: %s', transport)
        for _ in range(5):  # Retry a few times in case of PIN failures
            connection = self._defs.Client(transport=transport,
                                           ui=self.ui,
                                           state=self.__class__.cached_state)
            self._verify_version(connection)

            try:
                connection.ping(msg='', pin_protection=True)  # unlock PIN
                return connection
            except (self._defs.PinException, ValueError) as e:
                log.error('Invalid PIN: %s, retrying...', e)
                continue
            except Exception as e:
                log.exception('ping failed: %s', e)
                connection.close()  # so the next HID open() will succeed
                raise
623662
def open(self, mode='r', encoding=None):
        """"""
        access_type = self._get_access_type(mode)
        return open(self.localpath, 'r'+access_type, encoding=encoding)
730140
function open()
    {
        if (!$this->_opened) {
            $this->_opened = (bool)($this->_socket = @fsockopen(
                                                $this->_proto . $this->_ip,
                                                $this->_port,
                                                $errno,
                                                $errstr,
                                                $this->_timeout));
        }
        return $this->_opened;
    }
671098
def open(self):
        

        if not self._is_connected:
            
            print("Connecting to arduino on {}... ".format(self.device),end="")

            self.comm = serial.Serial()
            self.comm.port = self.device
            self.comm.baudrate = self.baud_rate
            self.comm.timeout = self.timeout
            self.dtr = self.enable_dtr
            self.comm.open()

            time.sleep(self.settle_time)
            self._is_connected = True

            print("done.")
588344
def main(argv):
    """"""
    port = pick_unused_port(pid=int(argv[1]) if len(argv) > 1 else None)
    if not port:
        sys.exit(1)
    print(port)
47351
private void open() throws ModbusIOException {
        if (commPort != null && !commPort.isOpen()) {
            setTimeout(timeout);
            try {
                commPort.open();
            }
            catch (IOException e) {
                throw new ModbusIOException(String.format("Cannot open port %s - %s", commPort.getDescriptivePortName(), e.getMessage()));
            }
        }
    }
717527
def open(self):
        
        LOGGER.info('Connecting to %s', self._url)
        asyncio.ensure_future(self._do_start())
915923
def open(self):
        """"""
        device_type = 'cisco_ios'
        if self.transport == 'telnet':
            device_type = 'cisco_ios_telnet'
        self.device = ConnectHandler(device_type=device_type,
                                     host=self.hostname,
                                     username=self.username,
                                     password=self.password,
                                     **self.netmiko_optional_args)
        # ensure in enable mode
        self.device.enable()
1166678
def open(self):
        """"""
        self._connection = paramiko.SSHClient()
        self._connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        try:
            self._connection.connect(hostname=self._hostname,
                                     username=self._username,
                                     password=self._password,
                                     timeout=self._timeout,
                                     port=self._port)
            self._connection.get_transport().set_keepalive(self._keepalive)
            self.connected = True
            self.config = PluribusConfig(self)
        except paramiko.ssh_exception.AuthenticationException:
            raise pyPluribus.exceptions.ConnectionError("Unable to open connection with {hostname}: \
                invalid credentials!".format(hostname=self._hostname))
        except socket_error as sockerr:
            raise pyPluribus.exceptions.ConnectionError("Cannot open connection: {skterr}. \
                Wrong port?".format(skterr=sockerr.message))
        except socket_gaierror as sockgai:
            raise pyPluribus.exceptions.ConnectionError("Cannot open connection: {gaierr}. \
                Wrong hostname?".format(gaierr=sockgai.message))
720549
def _netmiko_open(self, device_type, netmiko_optional_args=None):
        """"""
        if netmiko_optional_args is None:
            netmiko_optional_args = {}
        try:
            self._netmiko_device = ConnectHandler(
                device_type=device_type,
                host=self.hostname,
                username=self.username,
                password=self.password,
                timeout=self.timeout,
                **netmiko_optional_args
            )
        except NetMikoTimeoutException:
            raise ConnectionException("Cannot connect to {}".format(self.hostname))

        # ensure in enable mode
        self._netmiko_device.enable()
        return self._netmiko_device