Python java.lang.Exception() Examples

The following are 28 code examples of java.lang.Exception(). 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 java.lang , or try the search function .
Example #1
Source File: javaxml.py    From tcpproxy with MIT License 6 votes vote down vote up
def serial(self, data):
        if not self.is_jython:
            print ('[!] This module can only be used in jython!')
            return data
        try:
            # Creating XStream object and creating Java object from XML structure
            xs = XStream()
            serial = xs.fromXML(data)

            # writing created Java object to and serializing it with ObjectOutputStream
            bos = io.ByteArrayOutputStream()
            oos = io.ObjectOutputStream(bos)
            oos.writeObject(serial)

            # I had a problem with signed vs. unsigned bytes, hence the & 0xff
            return "".join([chr(x & 0xff) for x in bos.toByteArray().tolist()])
        except Exception as e:
            print ('[!] Caught Exception. Could not convert.\n')
            return data 
Example #2
Source File: javaxml.py    From tcpproxy with MIT License 6 votes vote down vote up
def deserial(self, data):
        if not self.is_jython:
            print ('[!] This module can only be used in jython!')
            return data

        try:
            # turn data into a Java object
            bis = io.ByteArrayInputStream(data)
            ois = io.ObjectInputStream(bis)
            obj = ois.readObject()

            # converting Java object to XML structure
            xs = XStream()
            xml = xs.toXML(obj)
            return xml
        except Exception as e:
            print ('[!] Caught Exception. Could not convert.\n')
            return data 
Example #3
Source File: socket.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def inet_pton(family, ip_string):
    try:
        if family == AF_INET:
            if not is_ipv4_address(ip_string):
                raise error("illegal IP address string passed to inet_pton")
        elif family == AF_INET6:
            if not is_ipv6_address(ip_string):
                raise error("illegal IP address string passed to inet_pton")
        else:
            raise error(errno.EAFNOSUPPORT, "Address family not supported by protocol")
        ia = java.net.InetAddress.getByName(ip_string)
        bytes = []
        for byte in ia.getAddress():
            if byte < 0:
                bytes.append(byte+256)
            else:
                bytes.append(byte)
        return "".join([chr(byte) for byte in bytes])
    except java.lang.Exception, jlx:
        raise _map_exception(jlx) 
Example #4
Source File: socket.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def recvfrom(self, num_bytes, flags=None):
        """
        There is some disagreement as to what the behaviour should be if
        a recvfrom operation is requested on an unbound socket.
        See the following links for more information
        http://bugs.jython.org/issue1005
        http://bugs.sun.com/view_bug.do?bug_id=6621689
        """
        try:
            # This is the old 2.1 behaviour
            #assert self.sock_impl
            # This is amak's preferred interpretation
            #raise error(errno.ENOTCONN, "Recvfrom on unbound udp socket meaningless operation")
            # And this is the option for cpython compatibility
            if not self.sock_impl:
                self.sock_impl = _datagram_socket_impl()
                self._config()
            return self.sock_impl.recvfrom(num_bytes, flags)
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #5
Source File: socket.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def accept(self):
        "This signifies a server socket"
        try:
            if not self.sock_impl:
                self.listen()
            assert self.server
            new_sock = self.sock_impl.accept()
            if not new_sock:
                raise would_block_error()
            cliconn = _tcpsocket()
            cliconn.pending_options[ (SOL_SOCKET, SO_REUSEADDR) ] = new_sock.jsocket.getReuseAddress()
            cliconn.sock_impl = new_sock
            cliconn._setup()
            return cliconn, new_sock.getpeername()
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #6
Source File: socket.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def recv(self, n):
        try:
            if not self.sock_impl: raise error(errno.ENOTCONN, 'Socket is not connected')
            if self.sock_impl.jchannel.isConnectionPending():
                self.sock_impl.jchannel.finishConnect()
            data = jarray.zeros(n, 'b')
            m = self.sock_impl.read(data)
            if m == -1:#indicates EOF has been reached, so we just return the empty string
                return ""
            elif m <= 0:
                if self.mode == MODE_NONBLOCKING:
                    raise would_block_error()
                return ""
            if m < n:
                data = data[:m]
            return data.tostring()
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #7
Source File: drv_javasax.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def parse(self, source):
        "Parse an XML document from a URL or an InputSource."
        try:
            self._parser.parse(JyInputSourceWrapper(source))
        except JException as e:
            # Handle the difference in how Oracle packages Xerces...
            if "MalformedByteSequenceException" in str(e):
                raise SAXUnicodeDecodeError(str(e))
            else:
                raise 
Example #8
Source File: drv_javasax.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def parse(self, source):
        "Parse an XML document from a URL or an InputSource."
        try:
            self._parser.parse(JyInputSourceWrapper(source))
        except JException as e:
            # Handle the difference in how Oracle packages Xerces...
            if "MalformedByteSequenceException" in str(e):
                raise SAXUnicodeDecodeError(str(e))
            else:
                raise 
Example #9
Source File: symbol.py    From vxhunter with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def demangled_symbol(symbol_string):
    sym_demangled_name = None
    sym_demangled = None
    if can_demangle:
        try:
            sym_demangled = demangler.demangle(symbol_string, True)

            if not sym_demangled:
                # some mangled function name didn't start with mangled prefix
                sym_demangled = demangler.demangle(symbol_string, False)

        except DemangledException as err:
            logger.debug("First pass demangling failed: symbol_string: {}, reason: {}".format(symbol_string, err))
            pass

        except java.lang.Exception as err:
            logger.debug("demangling failed: symbol_string: {}, reason: {}".format(symbol_string, err))

        if not sym_demangled:
            try:
                # Temp fix to handle _ prefix function name by remove _ prefix before demangle
                sym_demangled = demangler.demangle(symbol_string[1:], False)

            except DemangledException as err:
                logger.debug("Second pass demangling failed: symbol_string: {}, reason:{}".format(symbol_string, err))
                pass

            except java.lang.Exception as err:
                logger.debug("demangling failed: symbol_string: {}, reason: {}".format(symbol_string, err))

        if sym_demangled:
            sym_demangled_name = sym_demangled.getSignature(False)

            if sym_demangled_name:
                logger.debug("sym_demangled_name: {}".format(sym_demangled_name))
            else:
                logger.debug("Demangled symbol name for string {} is None.".format(symbol_string))

    return sym_demangled_name 
Example #10
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def _get_server_cert(self):
        try:
            return self.ssl_sock.getSession().getPeerCertificates()[0]
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #11
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def write(self, s):
        try:
            self._out_buf.write(s)
            self._out_buf.flush()
            return len(s)
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #12
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def read(self, n=4096):
        try:
            data = jarray.zeros(n, 'b')
            m = self._in_buf.read(data, 0, n)
            if m <= 0:
                return ""
            if m < n:
                data = data[:m]
            return data.tostring()
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #13
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def recv(self, num_bytes, flags=None):
        if not self.sock_impl: raise error(errno.ENOTCONN, "Socket is not connected")
        try:
            return self.sock_impl.recv(num_bytes, flags)
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #14
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def sendto(self, data, p1, p2=None):
        try:
            if not p2:
                flags, addr = 0, p1
            else:
                flags, addr = 0, p2
            if not self.sock_impl:
                self.sock_impl = _datagram_socket_impl()
                self._config()
            byte_array = java.lang.String(data).getBytes('iso-8859-1')
            result = self.sock_impl.sendto(byte_array, _get_jsockaddr(addr, self.family, self.type, self.proto, 0), flags)
            return result
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #15
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def _do_connect(self, addr):
        try:
            assert not self.connected, "Datagram Socket is already connected"
            if not self.sock_impl:
                self.sock_impl = _datagram_socket_impl()
                self._config()
            self.sock_impl.connect(_get_jsockaddr(addr, self.family, self.type, self.proto, 0))
            self.connected = True
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #16
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def bind(self, addr):
        try:            
            assert not self.sock_impl
            assert not self.local_addr
            # Do the address format check
            _get_jsockaddr(addr, self.family, self.type, self.proto, 0)
            self.local_addr = addr
            self.sock_impl = _datagram_socket_impl(_get_jsockaddr(self.local_addr, self.family, self.type, self.proto, AI_PASSIVE), 
                                                    self.pending_options[ (SOL_SOCKET, SO_REUSEADDR) ])
            self._config()
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #17
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def send(self, s):
        try:
            if not self.sock_impl: raise error(errno.ENOTCONN, 'Socket is not connected')
            if self.sock_impl.jchannel.isConnectionPending():
                self.sock_impl.jchannel.finishConnect()
            numwritten = self.sock_impl.write(s)
            if numwritten == 0 and self.mode == MODE_NONBLOCKING:
                raise would_block_error()
            return numwritten
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #18
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def _do_connect(self, addr):
        try:
            assert not self.sock_impl
            self.sock_impl = _client_socket_impl()
            if self.local_addr: # Has the socket been bound to a local address?
                self.sock_impl.bind(_get_jsockaddr(self.local_addr, self.family, self.type, self.proto, 0), 
                                     self.pending_options[ (SOL_SOCKET, SO_REUSEADDR) ])
            self._config() # Configure timeouts, etc, now that the socket exists
            self.sock_impl.connect(_get_jsockaddr(addr, self.family, self.type, self.proto, 0))
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #19
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def listen(self, backlog):
        "This signifies a server socket"
        try:
            assert not self.sock_impl
            self.server = 1
            self.sock_impl = _server_socket_impl(_get_jsockaddr(self.local_addr, self.family, self.type, self.proto, AI_PASSIVE), 
                                  backlog, self.pending_options[ (SOL_SOCKET, SO_REUSEADDR) ])
            self._config()
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #20
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def getsockname(self):
        try:
            if self.sock_impl is None:
                # If the user has already bound an address, return that
                if self.local_addr:
                    return self.local_addr
                # The user has not bound, connected or listened
                # This is what cpython raises in this scenario
                raise error(errno.EINVAL, "Invalid argument")
            return self.sock_impl.getsockname()
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #21
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def close(self):
        try:
            if self.sock_impl:
                self.sock_impl.close()
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #22
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def shutdown(self, how):
        assert how in (SHUT_RD, SHUT_WR, SHUT_RDWR)
        if not self.sock_impl:
            raise error(errno.ENOTCONN, "Transport endpoint is not connected")
        try:
            self.sock_impl.shutdown(how)
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #23
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def getsockopt(self, level, optname):
        try:
            if self.sock_impl:
                return self.sock_impl.getsockopt(level, optname)
            else:
                return self.pending_options.get( (level, optname), None)
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #24
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def setsockopt(self, level, optname, value):
        try:
            if self.sock_impl:
                self.sock_impl.setsockopt(level, optname, value)
            else:
                self.pending_options[ (level, optname) ] = value
        except java.lang.Exception, jlx:
            raise _map_exception(jlx) 
Example #25
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def getaddrinfo(host, port, family=AF_INET, socktype=None, proto=0, flags=0):
    try:
        if _ipv4_addresses_only:
            family = AF_INET
        if not family in [AF_INET, AF_INET6, AF_UNSPEC]:
            raise gaierror(errno.EIO, 'ai_family not supported')
        host = _getaddrinfo_get_host(host, family, flags)
        port = _getaddrinfo_get_port(port, flags)
        filter_fns = []
        filter_fns.append({
            AF_INET:   lambda x: isinstance(x, java.net.Inet4Address),
            AF_INET6:  lambda x: isinstance(x, java.net.Inet6Address),
            AF_UNSPEC: lambda x: isinstance(x, java.net.InetAddress),
        }[family])
        passive_mode = flags is not None and flags & AI_PASSIVE
        canonname_mode = flags is not None and flags & AI_CANONNAME
        results = []
        for a in java.net.InetAddress.getAllByName(host):
            if len([f for f in filter_fns if f(a)]):
                family = {java.net.Inet4Address: AF_INET, java.net.Inet6Address: AF_INET6}[a.getClass()]
                if passive_mode and not canonname_mode:
                    canonname = ""
                else:
                    canonname = asPyString(a.getCanonicalHostName())
                if host is None and passive_mode and not canonname_mode:
                    sockaddr = INADDR_ANY
                else:
                    sockaddr = asPyString(a.getHostAddress())
                # TODO: Include flowinfo and scopeid in a 4-tuple for IPv6 addresses
                sock_tuple = {AF_INET : _ipv4_address_t, AF_INET6 : _ipv6_address_t}[family](sockaddr, port, a)
                results.append((family, socktype, proto, canonname, sock_tuple))
        return results
    except java.lang.Exception, jlx:
        raise _map_exception(jlx) 
Example #26
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def _decode_idna(name, flags=0):
            try:
                jflags = 0
                if flags & NI_IDN_ALLOW_UNASSIGNED:
                    jflags |= au
                if flags & NI_IDN_USE_STD3_ASCII_RULES:
                    jflags |= usar
                return decode_fn(name, jflags)
            except Exception, x:
                raise UnicodeDecodeError(name) 
Example #27
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def gethostbyname(name):
    try:
        return asPyString(java.net.InetAddress.getByName(name).getHostAddress())
    except java.lang.Exception, jlx:
        raise _map_exception(jlx) 
Example #28
Source File: socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def gethostname():
    try:
        return asPyString(java.net.InetAddress.getLocalHost().getHostName())
    except java.lang.Exception, jlx:
        raise _map_exception(jlx)