Python errno.EPIPE Examples

The following are 30 code examples of errno.EPIPE(). 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: DNRelay.py    From rtp_cluster with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def deliver_dnotify(self, dnstring, _recurse = 0):
        if self.s == None:
            self.connect()
        if _recurse > _MAX_RECURSE:
            raise Exception('Cannot reconnect: %s', self.spath)
        if not dnstring.endswith('\n'):
            dnstring += '\n'
        while True:
            try:
                self.s.send(dnstring)
                break
            except socket.error as why:
                if why[0] == EINTR:
                    continue
                elif why[0] in (EPIPE, ENOTCONN, ECONNRESET):
                    self.s = None
                    return self.deliver_dnotify(dnstring, _recurse + 1)
                raise why
        # Clean any incoming data on the socket
        if len(self.poller.poll(0)) > 0:
            try:
                self.s.recv(1024)
            except:
                pass
        return 
Example #2
Source File: llcp-dta-iut.py    From nfcpy with European Union Public License 1.1 6 votes vote down vote up
def run(self):
        recv_socket = self.recv_socket
        send_socket = nfc.llcp.Socket(self.llc, nfc.llcp.LOGICAL_DATA_LINK)
        try:
            while True:
                log.info("waiting for start-of-test command")
                data, addr = recv_socket.recvfrom()
                if data == b'SOT':
                    break
            echo_out_addr = recv_socket.resolve('urn:nfc:sn:dta-cl-echo-out')
            while recv_socket.poll("recv"):
                log.info("received data, start delay time")
                time.sleep(self.options.cl_echo_delay)
                while recv_socket.poll("recv", timeout=0):
                    data, addr = recv_socket.recvfrom()
                    log.info("received %d byte from sap %d", len(data), addr)
                    send_socket.sendto(data, echo_out_addr)
                log.info("no more data, start waiting")
        except nfc.llcp.Error as e:
            (log.debug if e.errno == errno.EPIPE else log.error)(e)
        finally:
            log.info("close connection-less echo server socket")
            send_socket.close()
            recv_socket.close() 
Example #3
Source File: xmlrpclib.py    From meddle with MIT License 6 votes vote down vote up
def request(self, host, handler, request_body, verbose=0):
        #retry request once if cached connection has gone cold
        for i in (0, 1):
            try:
                return self.single_request(host, handler, request_body, verbose)
            except socket.error, e:
                if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE):
                    raise
            except httplib.BadStatusLine: #close after we sent request
                if i:
                    raise

    ##
    # Send a complete request, and parse the response.
    #
    # @param host Target host.
    # @param handler Target PRC handler.
    # @param request_body XML-RPC request body.
    # @param verbose Debugging flag.
    # @return Parsed response. 
Example #4
Source File: stones.py    From Computable with MIT License 6 votes vote down vote up
def main(logfile):
    p = hotshot.Profile(logfile)
    benchtime, stones = p.runcall(test.pystone.pystones)
    p.close()

    print "Pystone(%s) time for %d passes = %g" % \
          (test.pystone.__version__, test.pystone.LOOPS, benchtime)
    print "This machine benchmarks at %g pystones/second" % stones

    stats = hotshot.stats.load(logfile)
    stats.strip_dirs()
    stats.sort_stats('time', 'calls')
    try:
        stats.print_stats(20)
    except IOError, e:
        if e.errno != errno.EPIPE:
            raise 
Example #5
Source File: xmlrpclib.py    From BinderFilter with MIT License 6 votes vote down vote up
def request(self, host, handler, request_body, verbose=0):
        #retry request once if cached connection has gone cold
        for i in (0, 1):
            try:
                return self.single_request(host, handler, request_body, verbose)
            except socket.error, e:
                if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE):
                    raise
            except httplib.BadStatusLine: #close after we sent request
                if i:
                    raise

    ##
    # Send a complete request, and parse the response.
    #
    # @param host Target host.
    # @param handler Target PRC handler.
    # @param request_body XML-RPC request body.
    # @param verbose Debugging flag.
    # @return Parsed response. 
Example #6
Source File: sagemaker_pipe.py    From sagemaker-xgboost-container with Apache License 2.0 6 votes vote down vote up
def run_pipe(channel, src_retriever, dest):
    """Emulate SageMaker pipe.

    :param channel: Pipe name
    :param src_retriever: Function to stream data to the pipe
    :param dest: Path to directory of the pipe
    """
    for epoch in range(NUM_EPOCHS):
        print('Running epoch: {}'.format(epoch))
        # delete previous epoch's fifo if it exists:
        delete_fifo(dest, channel, epoch - 1)

        try:
            fifo_pth = create_fifo(dest, channel, epoch)
            with open(fifo_pth, mode='bw', buffering=0) as fifo:
                src_retriever(fifo)
        except IOError as e:
            if e.errno == errno.EPIPE:
                print("Client closed current epoch's pipe before reaching EOF. "
                      "Continuing with next epoch...")
            else:
                raise
        finally:
            delete_fifo(dest, channel, epoch)
    print('Completed pipe for channel: {}'.format(channel)) 
Example #7
Source File: stones.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def main(logfile):
    p = hotshot.Profile(logfile)
    benchtime, stones = p.runcall(test.pystone.pystones)
    p.close()

    print "Pystone(%s) time for %d passes = %g" % \
          (test.pystone.__version__, test.pystone.LOOPS, benchtime)
    print "This machine benchmarks at %g pystones/second" % stones

    stats = hotshot.stats.load(logfile)
    stats.strip_dirs()
    stats.sort_stats('time', 'calls')
    try:
        stats.print_stats(20)
    except IOError, e:
        if e.errno != errno.EPIPE:
            raise 
Example #8
Source File: llcp-test-server.py    From nfcpy with European Union Public License 1.1 6 votes vote down vote up
def listen(self, socket):
        try:
            socket.setsockopt(nfc.llcp.SO_RCVBUF, 2)
            while socket.poll("recv"):
                log.info("data available, start delay time")
                time.sleep(2.0)
                while socket.poll("recv", timeout=0):
                    data, addr = socket.recvfrom()
                    log.info("received {0} byte from sap {1}"
                             .format(len(data), addr))
                    socket.sendto(data, addr)
                log.info("no more data, start waiting")
        except nfc.llcp.Error as e:
            (log.debug if e.errno == errno.EPIPE else log.error)(e)
        finally:
            log.info("close connection-less echo server socket")
            socket.close() 
Example #9
Source File: test_llcp_tco.py    From nfcpy with European Union Public License 1.1 6 votes vote down vote up
def test_recvfrom(self, tco):
        pdu = nfc.llcp.pdu.Symmetry()
        assert tco.enqueue(pdu) is False
        pdu = nfc.llcp.pdu.UnnumberedInformation(1, 1, (tco.recv_miu+1) * b'1')
        assert tco.enqueue(pdu) is False
        pdu = nfc.llcp.pdu.UnnumberedInformation(1, 1, HEX('1122'))
        assert tco.enqueue(pdu) is True
        assert tco.recvfrom() == (pdu.data, pdu.ssap)
        threading.Timer(0.01, tco.close).start()
        with pytest.raises(nfc.llcp.Error) as excinfo:
            tco.recvfrom()
        assert excinfo.value.errno == errno.EPIPE
        with pytest.raises(nfc.llcp.Error) as excinfo:
            tco.recvfrom()
        assert excinfo.value.errno == errno.ESHUTDOWN


# =============================================================================
# Data Link Connection
# ============================================================================= 
Example #10
Source File: test_llcp_tco.py    From nfcpy with European Union Public License 1.1 6 votes vote down vote up
def test_accept(self, tco):
        with pytest.raises(nfc.llcp.Error) as excinfo:
            tco.accept()
        assert excinfo.value.errno == errno.EINVAL
        tco.setsockopt(nfc.llcp.SO_RCVMIU, 1000)
        tco.setsockopt(nfc.llcp.SO_RCVBUF, 2)
        tco.listen(backlog=1)
        assert tco.state.LISTEN is True
        tco.enqueue(nfc.llcp.pdu.Connect(tco.addr, 17, 500, 15))
        dlc = tco.accept()
        assert isinstance(dlc, nfc.llcp.tco.DataLinkConnection)
        assert dlc.state.ESTABLISHED is True
        assert dlc.getsockopt(nfc.llcp.SO_RCVMIU) == 1000
        assert dlc.getsockopt(nfc.llcp.SO_SNDMIU) == 500
        assert dlc.getsockopt(nfc.llcp.SO_RCVBUF) == 2
        assert tco.dequeue(128, 4) == \
            nfc.llcp.pdu.ConnectionComplete(17, tco.addr, 1000, 2)
        threading.Timer(0.01, tco.close).start()
        with pytest.raises(nfc.llcp.Error) as excinfo:
            tco.accept()
        assert excinfo.value.errno == errno.EPIPE
        with pytest.raises(nfc.llcp.Error) as excinfo:
            tco.accept()
        assert excinfo.value.errno == errno.ESHUTDOWN 
Example #11
Source File: xmlrpclib.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def request(self, host, handler, request_body, verbose=0):
        #retry request once if cached connection has gone cold
        for i in (0, 1):
            try:
                return self.single_request(host, handler, request_body, verbose)
            except socket.error, e:
                if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE):
                    raise
            except httplib.BadStatusLine: #close after we sent request
                if i:
                    raise

    ##
    # Send a complete request, and parse the response.
    #
    # @param host Target host.
    # @param handler Target PRC handler.
    # @param request_body XML-RPC request body.
    # @param verbose Debugging flag.
    # @return Parsed response. 
Example #12
Source File: tfr_read.py    From spotify-tensorflow with Apache License 2.0 6 votes vote down vote up
def main():
    parser = argparse.ArgumentParser(description="Output TFRecords as JSON")
    parser.add_argument("-s", "--schema", help="Path to Schema protobuf file. Uses Example if not "
                                               "supplied.")
    parser.add_argument("tf_records_paths",
                        metavar="TF_RECORDS_PATH",
                        nargs="+",
                        help="TFRecords file (or directory containing .tfrecords files)")

    args = parser.parse_args()

    try:
        for json_str in tfr_read_to_json(args.tf_records_paths, args.schema):
            print(json_str)
    except IOError as e:
        if e.errno == errno.EPIPE:
            sys.exit(0)
        raise e 
Example #13
Source File: stones.py    From BinderFilter with MIT License 6 votes vote down vote up
def main(logfile):
    p = hotshot.Profile(logfile)
    benchtime, stones = p.runcall(test.pystone.pystones)
    p.close()

    print "Pystone(%s) time for %d passes = %g" % \
          (test.pystone.__version__, test.pystone.LOOPS, benchtime)
    print "This machine benchmarks at %g pystones/second" % stones

    stats = hotshot.stats.load(logfile)
    stats.strip_dirs()
    stats.sort_stats('time', 'calls')
    try:
        stats.print_stats(20)
    except IOError, e:
        if e.errno != errno.EPIPE:
            raise 
Example #14
Source File: tco.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def recv(self):
        if self.state.SHUTDOWN:
            raise err.Error(errno.ESHUTDOWN)
        try:
            return super(RawAccessPoint, self).recv()
        except IndexError:
            raise err.Error(errno.EPIPE) 
Example #15
Source File: llcp-dta-iut.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def run(self):
        try:
            while self.socket.poll('recv'):
                data, addr = self.socket.recvfrom()
                log.debug("received %d byte from sap %d", len(data), addr)
                if len(data) == 6 and data.startswith(b'\xFF\x00\x00\x00'):
                    pattern_number = struct.unpack_from('!H', data, 4)[0]
                    log.info("received pattern number %02Xh", pattern_number)
                    self.options.pattern_number = pattern_number
        except nfc.llcp.Error as e:
            (log.debug if e.errno == errno.EPIPE else log.error)(e)
        finally:
            self.socket.close() 
Example #16
Source File: phdc-test-manager.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def listen(self, socket):
        try:
            while True:
                client = socket.accept()
                peer = client.getpeername()
                miu = client.getsockopt(nfc.llcp.SO_SNDMIU)
                log.info("serving phdc agent from sap {0}".format(peer))
                log.info("entering ieee manager")
                while True:
                    data = client.recv()
                    if data is None:
                        break
                    log.info("rcvd {0} byte data".format(len(data)))
                    size = struct.unpack(">H", data[0:2])[0]
                    apdu = data[2:]
                    while len(apdu) < size:
                        data = client.recv()
                        if data is None:
                            break
                        log.info("rcvd {0} byte data".format(len(data)))
                        apdu += data
                    log.info("[ieee] <<< {0}".format(hexlify(apdu).decode()))
                    if apdu.startswith(b"\xE2\x00"):
                        apdu = bytearray.fromhex(thermometer_assoc_res)
                    elif apdu.startswith(b"\xE4\x00"):
                        apdu = bytearray.fromhex(assoc_release_res)
                    else:
                        apdu = apdu[::-1]
                    time.sleep(0.2)
                    log.info("[ieee] >>> {0}".format(hexlify(apdu).decode()))
                    data = struct.pack(">H", len(apdu)) + apdu
                    for i in range(0, len(data), miu):
                        client.send(bytes(data[i:i + miu]))
                log.info("remote peer {0} closed connection".format(peer))
                log.info("leaving ieee manager")
                client.close()

        except nfc.llcp.Error as e:
            (log.debug if e.errno == errno.EPIPE else log.error)(e)
        finally:
            socket.close() 
Example #17
Source File: ps_mem.py    From malfs-milis with MIT License 5 votes vote down vote up
def std_exceptions(etype, value, tb):
    sys.excepthook = sys.__excepthook__
    if issubclass(etype, KeyboardInterrupt):
        pass
    elif issubclass(etype, IOError) and value.errno == errno.EPIPE:
        pass
    else:
        sys.__excepthook__(etype, value, tb) 
Example #18
Source File: pipeline.py    From Comparative-Annotation-Toolkit with Apache License 2.0 5 votes vote down vote up
def __writer(self):
        "write thread function"
        try:
            self.fifo.getWfh().write(self.data)
            self.fifo.wclose()
        except IOError as ex:
            if ex.errno != errno.EPIPE:
                raise 
Example #19
Source File: tco.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def recvfrom(self):
        if self.state.SHUTDOWN:
            raise err.Error(errno.ESHUTDOWN)
        try:
            rcvd_pdu = super(LogicalDataLink, self).recv()
        except IndexError:
            raise err.Error(errno.EPIPE)
        return (rcvd_pdu.data, rcvd_pdu.ssap) if rcvd_pdu else (None, None) 
Example #20
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def __init__(self, text, pipe_trigger):
        # When sendall() is called with pipe_trigger, raise EPIPE.
        FakeSocket.__init__(self, text)
        self.pipe_trigger = pipe_trigger 
Example #21
Source File: test_httplib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def sendall(self, data):
        if self.pipe_trigger in data:
            raise socket.error(errno.EPIPE, "gotcha")
        self.data += data 
Example #22
Source File: server.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def serve(self, socket):
        peer_sap = socket.getpeername()
        log.info("serving handover client on remote sap {0}".format(peer_sap))
        send_miu = socket.getsockopt(nfc.llcp.SO_SNDMIU)
        try:
            while socket.poll("recv"):
                request = bytearray()
                while socket.poll("recv"):
                    request += socket.recv()

                    if len(request) == 0:
                        continue  # need some data

                    try:
                        list(ndef.message_decoder(request, 'strict', {}))
                    except ndef.DecodeError:
                        continue  # need more data

                    response = self._process_request_data(request)

                    for offset in range(0, len(response), send_miu):
                        fragment = response[offset:offset + send_miu]
                        if not socket.send(fragment):
                            return  # connection closed

        except nfc.llcp.Error as error:
            (log.debug if error.errno == errno.EPIPE else log.error)(error)
        finally:
            socket.close()
            log.debug("handover serve thread terminated") 
Example #23
Source File: llcp-dta-iut.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def run(self):
        try:
            self.listen_socket.listen(backlog=0)
            while True:
                socket = self.listen_socket.accept()
                srcsap = socket.getpeername()
                log.info("accepted data link connection from sap %d", srcsap)
                self.recv(socket, socket.llc)
        except nfc.llcp.Error as e:
            (log.debug if e.errno == errno.EPIPE else log.error)(e)
        finally:
            log.info("close connection-mode echo server socket")
            self.listen_socket.close() 
Example #24
Source File: llcp-test-server.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def listen(self, socket):
        try:
            socket.setsockopt(nfc.llcp.SO_RCVBUF, 2)
            socket.listen(backlog=4)
            while True:
                client_socket = socket.accept()
                peer = client_socket.getpeername()
                log.info("client sap {0} connected".format(peer))
                threading.Thread(target=self.serve,
                                 args=(client_socket,)).start()
        except nfc.llcp.Error as e:
            (log.debug if e.errno == errno.EPIPE else log.error)(e)
        finally:
            log.info("close connection-mode dump server socket")
            socket.close() 
Example #25
Source File: printing.py    From aegea with Apache License 2.0 5 votes vote down vote up
def page_output(content, pager=None, file=None):
    if file is None:
        file = sys.stdout
    if not content.endswith("\n"):
        content += "\n"

    pager_process = None
    try:
        if file != sys.stdout or not file.isatty() or not content.startswith(border("┌")):
            raise AegeaException()
        content_lines = content.splitlines()
        content_rows = len(content_lines)

        tty_cols, tty_rows = get_terminal_size()

        naive_content_cols = max(len(i) for i in content_lines)
        if tty_rows > content_rows and tty_cols > naive_content_cols:
            raise AegeaException()

        content_cols = max(len(strip_ansi_codes(i)) for i in content_lines)
        if tty_rows > content_rows and tty_cols > content_cols:
            raise AegeaException()

        pager_process = subprocess.Popen(pager or os.environ.get("PAGER", "less -RS"), shell=True,
                                         stdin=subprocess.PIPE, stdout=file)
        pager_process.stdin.write(content.encode("utf-8"))
        pager_process.stdin.close()
        pager_process.wait()
        if pager_process.returncode != os.EX_OK:
            raise AegeaException()
    except Exception as e:
        if not (isinstance(e, IOError) and e.errno == errno.EPIPE):
            file.write(content.encode("utf-8") if USING_PYTHON2 else content)
    finally:
        try:
            pager_process.terminate()
        except BaseException:
            pass 
Example #26
Source File: tco.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def accept(self):
        with self.lock:
            if self.state.SHUTDOWN:
                raise err.Error(errno.ESHUTDOWN)
            if not self.state.LISTEN:
                self.err("accept() but socket state is {0}".format(self.state))
                raise err.Error(errno.EINVAL)
            self.recv_buf += 1
            try:
                rcvd_pdu = super(DataLinkConnection, self).recv()
            except IndexError:
                raise err.Error(errno.EPIPE)
            self.recv_buf -= 1
            if rcvd_pdu.name == "CONNECT":
                dlc = DataLinkConnection(self.recv_miu, self.recv_win)
                dlc.addr = self.addr
                dlc.peer = rcvd_pdu.ssap
                dlc.send_miu = rcvd_pdu.miu
                dlc.send_win = rcvd_pdu.rw
                send_pdu = pdu.ConnectionComplete(dlc.peer, dlc.addr)
                send_pdu.miu, send_pdu.rw = dlc.recv_miu, dlc.recv_win
                log.debug("accepting CONNECT from SAP %d" % dlc.peer)
                dlc.state.ESTABLISHED = True
                self.send_queue.append(send_pdu)
                return dlc
            else:  # pragma: no cover
                raise RuntimeError("CONNECT expected, not " + rcvd_pdu.name) 
Example #27
Source File: subprocess.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def communicate(self, input=None):
        """Interact with process: Send data to stdin.  Read data from
        stdout and stderr, until end-of-file is reached.  Wait for
        process to terminate.  The optional input argument should be a
        string to be sent to the child process, or None, if no data
        should be sent to the child.

        communicate() returns a tuple (stdout, stderr)."""

        # Optimization: If we are only using one pipe, or no pipe at
        # all, using select() or threads is unnecessary.
        if [self.stdin, self.stdout, self.stderr].count(None) >= 2:
            stdout = None
            stderr = None
            if self.stdin:
                if input:
                    try:
                        self.stdin.write(input)
                    except IOError as e:
                        if e.errno != errno.EPIPE and e.errno != errno.EINVAL:
                            raise
                self.stdin.close()
            elif self.stdout:
                stdout = _eintr_retry_call(self.stdout.read)
                self.stdout.close()
            elif self.stderr:
                stderr = _eintr_retry_call(self.stderr.read)
                self.stderr.close()
            self.wait()
            return (stdout, stderr)

        return self._communicate(input) 
Example #28
Source File: test_functional.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def send_check_error(self, to_send):
            # Unlike inet domain sockets, Unix domain sockets can trigger a
            # 'Broken pipe' error when the socket it closed.
            try:
                self.sock.send(to_send)
            except socket.error as exc:
                self.assertEqual(get_errno(exc), errno.EPIPE) 
Example #29
Source File: server.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def listen(self, llc, socket):
        log.debug("handover listen thread started")
        try:
            while True:
                client_socket = socket.accept()
                client_thread = threading.Thread(target=self.serve,
                                                 args=(client_socket,))
                client_thread.start()
        except nfc.llcp.Error as error:
            (log.debug if error.errno == errno.EPIPE else log.error)(error)
        finally:
            socket.close()
            log.debug("handover listen thread terminated") 
Example #30
Source File: gthread.py    From jbox with MIT License 5 votes vote down vote up
def handle(self, conn):
        keepalive = False
        req = None
        try:
            req = six.next(conn.parser)
            if not req:
                return (False, conn)

            # handle the request
            keepalive = self.handle_request(req, conn)
            if keepalive:
                return (keepalive, conn)
        except http.errors.NoMoreData as e:
            self.log.debug("Ignored premature client disconnection. %s", e)

        except StopIteration as e:
            self.log.debug("Closing connection. %s", e)
        except ssl.SSLError as e:
            if e.args[0] == ssl.SSL_ERROR_EOF:
                self.log.debug("ssl connection closed")
                conn.sock.close()
            else:
                self.log.debug("Error processing SSL request.")
                self.handle_error(req, conn.sock, conn.client, e)

        except EnvironmentError as e:
            if e.errno not in (errno.EPIPE, errno.ECONNRESET):
                self.log.exception("Socket error processing request.")
            else:
                if e.errno == errno.ECONNRESET:
                    self.log.debug("Ignoring connection reset")
                else:
                    self.log.debug("Ignoring connection epipe")
        except Exception as e:
            self.handle_error(req, conn.sock, conn.client, e)

        return (False, conn)