def _transmitMsg(self, msg):
"""Send an OSC message over a streaming socket. Raises exception if it
should fail. If everything is transmitted properly, True is returned. If
socket has been closed, False.
"""
if not isinstance(msg, OSCMessage):
raise TypeError("'msg' argument is not an OSCMessage or OSCBundle object")
try:
binary = msg.getBinary()
length = len(binary)
# prepend length of packet before the actual message (big endian)
len_big_endian = array.array('c', '\0' * 4)
struct.pack_into(">L", len_big_endian, 0, length)
len_big_endian = len_big_endian.tostring()
if self._transmit(len_big_endian) and self._transmit(binary):
return True
return False
except socket.error as e:
if e[0] == errno.EPIPE: # broken pipe
return False
raise e
python类EPIPE的实例源码
def _transmitMsg(self, msg):
"""Send an OSC message over a streaming socket. Raises exception if it
should fail. If everything is transmitted properly, True is returned. If
socket has been closed, False.
"""
if not isinstance(msg, OSCMessage):
raise TypeError("'msg' argument is not an OSCMessage or OSCBundle object")
try:
binary = msg.getBinary()
length = len(binary)
# prepend length of packet before the actual message (big endian)
len_big_endian = array.array('c', '\0' * 4)
struct.pack_into(">L", len_big_endian, 0, length)
len_big_endian = len_big_endian.tostring()
if self._transmit(len_big_endian) and self._transmit(binary):
return True
return False
except socket.error, e:
if e[0] == errno.EPIPE: # broken pipe
return False
raise e
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.
def run_with_args(args, parser):
# type: (argparse.Namespace, argparse.ArgumentParser) -> int
set_logging_parameters(args, parser)
start_time = time.time()
ret = OK
try:
if args.profile:
outline("Profiling...")
profile("ret = whatstyle(args, parser)", locals(), globals())
else:
ret = whatstyle(args, parser)
except IOError as exc:
# If the output is piped into a pager like 'less' we get a broken pipe when
# the pager is quit early and that is ok.
if exc.errno == errno.EPIPE:
pass
elif str(exc) == 'Stream closed':
pass
else:
raise
if not PY2:
sys.stderr.close()
iprint(INFO_TIME, 'Run time: %s seconds' % (time.time() - start_time))
return ret
def run(self):
"""Runs the handler, flushes the streams, and ends the request."""
try:
protocolStatus, appStatus = self.server.handler(self)
except:
traceback.print_exc(file=self.stderr)
self.stderr.flush()
if not self.stdout.dataWritten:
self.server.error(self)
protocolStatus, appStatus = FCGI_REQUEST_COMPLETE, 0
if __debug__: _debug(1, 'protocolStatus = %d, appStatus = %d' %
(protocolStatus, appStatus))
try:
self._flush()
self._end(appStatus, protocolStatus)
except socket.error, e:
if e[0] != errno.EPIPE:
raise
def __init__(self, fh, map=None, maxdata=None, ignore_broken_pipe=False, logger=None, **obsopt):
"""Wrap a dispatcher around the passed filehandle.
If `ignore_broken_pipe` is `True`, an `EPIPE` or `EBADF` error will
call `handle_close()` instead of `handle_expt()`. Useful when broken
pipes should be handled quietly.
`logger` is a logger which will be used to log unusual exceptions;
otherwise, they will be printed to stderr.
"""
self.maxdata = maxdata if maxdata else self.pipe_maxdata
self.__logger = logger
if ignore_broken_pipe:
self.__ignore_errno = [EPIPE, EBADF]
else:
self.__ignore_errno = []
self.__filehandle = fh
# Check for overduplication of the file descriptor and close the extra
fddup = os.dup(fh.fileno())
file_dispatcher.__init__(self, fddup, map=map)
if (self._fileno != fddup): os.close (fddup)
Observable.__init__(self, **obsopt)
def writeSomeData(self, data):
"""Write some data to the open process.
"""
try:
rv = os.write(self.fd, data)
if rv == len(data) and self.enableReadHack:
self.startReading()
return rv
except IOError, io:
if io.args[0] == errno.EAGAIN:
return 0
return CONNECTION_LOST
except OSError, ose:
if ose.errno == errno.EPIPE:
return CONNECTION_LOST
if ose.errno == errno.EAGAIN: # MacOS-X does this
return 0
raise
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.
def test_recv(self, tco):
pdu = nfc.llcp.pdu.UnnumberedInformation(1, 1, HEX('1122'))
assert tco.enqueue(pdu) is True
assert tco.recv() == pdu
threading.Timer(0.01, tco.close).start()
with pytest.raises(nfc.llcp.Error) as excinfo:
tco.recv()
assert excinfo.value.errno == errno.EPIPE
with pytest.raises(nfc.llcp.Error) as excinfo:
tco.recv()
assert excinfo.value.errno == errno.ESHUTDOWN
# =============================================================================
# Logical Data Link
# =============================================================================
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
# =============================================================================
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
def send(self, message, flags):
with self.send_token:
if not self.state.ESTABLISHED:
self.err("send() in socket state {0}".format(self.state))
if self.state.CLOSE_WAIT:
raise err.Error(errno.EPIPE)
raise err.Error(errno.ENOTCONN)
if len(message) > self.send_miu:
raise err.Error(errno.EMSGSIZE)
while self.send_window_slots == 0 and self.state.ESTABLISHED:
if flags & nfc.llcp.MSG_DONTWAIT:
raise err.Error(errno.EWOULDBLOCK)
self.log("waiting on busy send window")
self.send_token.wait()
self.log("send {0} byte on {1}".format(len(message), str(self)))
if self.state.ESTABLISHED:
send_pdu = pdu.Information(self.peer, self.addr, data=message)
send_pdu.ns = self.send_cnt
self.send_cnt = (self.send_cnt + 1) % 16
super(DataLinkConnection, self).send(send_pdu, flags)
return self.state.ESTABLISHED is True
def _run_gtp_session(engine, read, write):
while True:
try:
line = read()
except EOFError:
break
response, end_session = engine.handle_line(line)
if response is not None:
try:
write(response)
except IOError, e:
if e.errno == errno.EPIPE:
raise ControllerDisconnected(*e.args)
else:
raise
if end_session:
break
def test_broken_pipe_recovery(self):
"""
Tests handling of a Broken Pipe (which interacts with an httplib bug)
"""
exception = IOError(errno.EPIPE, "Broken pipe")
harness = CallbackTestHarness(exception=exception)
res_upload_handler = ResumableUploadHandler(num_retries=1)
small_src_file_as_string, small_src_file = self.make_small_file()
small_src_file.seek(0)
dst_key = self._MakeKey(set_contents=False)
dst_key.set_contents_from_file(
small_src_file, cb=harness.call,
res_upload_handler=res_upload_handler)
# Ensure uploaded object has correct content.
self.assertEqual(SMALL_KEY_SIZE, dst_key.size)
self.assertEqual(small_src_file_as_string,
dst_key.get_contents_as_string())
def test_broken_pipe_recovery(self):
"""
Tests handling of a Broken Pipe (which interacts with an httplib bug)
"""
exception = IOError(errno.EPIPE, "Broken pipe")
harness = CallbackTestHarness(exception=exception)
res_download_handler = ResumableDownloadHandler(num_retries=1)
dst_fp = self.make_dst_fp()
small_src_key_as_string, small_src_key = self.make_small_key()
small_src_key.get_contents_to_file(
dst_fp, cb=harness.call,
res_download_handler=res_download_handler)
# Ensure downloaded object has correct content.
self.assertEqual(SMALL_KEY_SIZE,
get_cur_file_size(dst_fp))
self.assertEqual(small_src_key_as_string,
small_src_key.get_contents_as_string())
def __send_message(self, msg):
"""
Write a message to the socket.
:param msg: The message to send (max. 1024 bytes).
"""
if self.__socket is None:
return
try:
self.__socket.send(msg)
logger.debug('Message send: %s ' % msg)
except IOError as e:
if e.errno == errno.EPIPE:
logger.warning('Unable send message "%s". Broken pipe.' % msg)
else:
logger.exception(e)
def cmd_obj(self, qmp_cmd):
"""
Send a QMP command to the QMP Monitor.
@param qmp_cmd: QMP command to be sent as a Python dict
@return QMP response as a Python dict or None if the connection has
been closed
"""
if self._debug:
print >>sys.stderr, "QMP:>>> %s" % qmp_cmd
try:
self.__sock.sendall(json.dumps(qmp_cmd))
except socket.error as err:
if err[0] == errno.EPIPE:
return
raise socket.error(err)
resp = self.__json_read()
if self._debug:
print >>sys.stderr, "QMP:<<< %s" % resp
return resp
def run():
"""Execute main loop."""
try:
setup()
try:
parser, args = make_argparser()
mainloop(parser, args)
except KeyboardInterrupt as exc:
sys.stderr.flush()
sys.exit(2)
except IOError as exc:
if exc.errno == errno.EPIPE: # downstream is done with our output
sys.stderr.flush()
sys.exit(0)
else:
raise
finally:
logging.shutdown()
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
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.
def main():
p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r')
total, d = None, {}
for line in p.readlines():
i = 0
while line[i] in '0123456789': i = i+1
size = eval(line[:i])
while line[i] in ' \t': i = i+1
filename = line[i:-1]
comps = filename.split('/')
if comps[0] == '': comps[0] = '/'
if comps[len(comps)-1] == '': del comps[len(comps)-1]
total, d = store(size, comps, total, d)
try:
display(total, d)
except IOError, e:
if e.errno != errno.EPIPE:
raise
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
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.
def main():
p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r')
total, d = None, {}
for line in p.readlines():
i = 0
while line[i] in '0123456789': i = i+1
size = eval(line[:i])
while line[i] in ' \t': i = i+1
filename = line[i:-1]
comps = filename.split('/')
if comps[0] == '': comps[0] = '/'
if comps[len(comps)-1] == '': del comps[len(comps)-1]
total, d = store(size, comps, total, d)
try:
display(total, d)
except IOError, e:
if e.errno != errno.EPIPE:
raise
def write(self, data):
if hasattr(data, "tobytes"):
data = data.tobytes()
with self._parent._streamLock:
oldPos = self._stream.pos
self._stream.pos = self._stream.len
self._stream.write(data)
self._stream.pos = oldPos
while self._stream.len > 0 and not self._streamClosed:
self._parent._processStreams()
if self._streamClosed:
self._closed = True
if self._stream.len != 0:
raise IOError(errno.EPIPE,
"Could not write all data to stream")
return len(data)
def writeSomeData(self, data):
"""Write some data to the open process.
"""
try:
rv = os.write(self.fd, data)
if rv == len(data) and self.enableReadHack:
self.startReading()
return rv
except IOError, io:
if io.args[0] == errno.EAGAIN:
return 0
return CONNECTION_LOST
except OSError, ose:
if ose.errno == errno.EPIPE:
return CONNECTION_LOST
if ose.errno == errno.EAGAIN: # MacOS-X does this
return 0
raise
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.
def accept(self):
with self.lock:
if self.state.SHUTDOWN:
raise Error(errno.EBADF)
if not self.state.LISTEN:
self.err("accept() but socket state is {0}".format(self.state))
raise Error(errno.EINVAL)
self.recv_buf += 1
try: pdu = super(DataLinkConnection, self).recv()
except IndexError: raise Error(errno.EPIPE)
self.recv_buf -= 1
if isinstance(pdu, Connect):
dlc = DataLinkConnection(self.recv_miu, self.recv_win)
dlc.addr = self.addr
dlc.peer = pdu.ssap
dlc.send_miu = pdu.miu
dlc.send_win = pdu.rw
pdu = ConnectionComplete(dlc.peer, dlc.addr)
pdu.miu, pdu.rw = dlc.recv_miu, dlc.recv_win
log.info("accepting CONNECT from SAP %d" % dlc.peer)
dlc.state.ESTABLISHED = True
self.send_queue.append(pdu)
return dlc
raise RuntimeError("only CONNECT expected, not "+ pdu.name)
def send(self, message):
with self.send_token:
if not self.state.ESTABLISHED:
self.err("send() in socket state {0}".format(self.state))
if self.state.CLOSE_WAIT:
raise Error(errno.EPIPE)
raise Error(errno.ENOTCONN)
if len(message) > self.send_miu:
raise Error(errno.EMSGSIZE)
while self.send_window_slots == 0 and self.state.ESTABLISHED:
self.log("waiting on busy send window")
self.send_token.wait()
self.log("send() {0}".format(str(self)))
if self.state.ESTABLISHED:
pdu = Information(self.peer, self.addr, sdu=message)
pdu.ns = self.send_cnt
self.send_cnt = (self.send_cnt + 1) % 16
super(DataLinkConnection, self).send(pdu)
return self.state.ESTABLISHED == True
def _connect(self):
logger.debug('ARI client listening...')
try:
with self._running():
self.client.run(apps=[APPLICATION_NAME])
except socket.error as e:
if e.errno == errno.EPIPE:
# bug in ari-py when calling client.close(): ignore it and stop
logger.error('Error while listening for ARI events: %s', e)
return
else:
self._connection_error(e)
except (WebSocketException, HTTPError) as e:
self._connection_error(e)
except ValueError:
logger.warning('Received non-JSON message from ARI... disconnecting')
self.client.close()