Python socket.socketpair() Examples
The following are 30
code examples of socket.socketpair().
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
socket
, or try the search function
.
Example #1
Source File: connection.py From BinderFilter with MIT License | 8 votes |
def Pipe(duplex=True): ''' Returns pair of connection objects at either end of a pipe ''' if duplex: s1, s2 = socket.socketpair() s1.setblocking(True) s2.setblocking(True) c1 = _multiprocessing.Connection(os.dup(s1.fileno())) c2 = _multiprocessing.Connection(os.dup(s2.fileno())) s1.close() s2.close() else: fd1, fd2 = os.pipe() c1 = _multiprocessing.Connection(fd1, writable=False) c2 = _multiprocessing.Connection(fd2, readable=False) return c1, c2
Example #2
Source File: _signal.py From fbs with GNU General Public License v3.0 | 7 votes |
def __init__(self, app, QAbstractSocket): self._app = app self.old_fd = None # Create a socket pair self.wsock, self.rsock = socketpair(type=SOCK_DGRAM) self.socket = QAbstractSocket(QAbstractSocket.UdpSocket, app) # Let Qt listen on the one end self.socket.setSocketDescriptor(self.rsock.fileno()) # And let Python write on the other end self.wsock.setblocking(False) self.old_fd = signal.set_wakeup_fd(self.wsock.fileno()) # First Python code executed gets any exception from # the signal handler, so add a dummy handler first self.socket.readyRead.connect(lambda : None) # Second handler does the real handling self.socket.readyRead.connect(self._readSignal)
Example #3
Source File: ioloop_test.py From teleport with Apache License 2.0 | 6 votes |
def test_read_while_writeable(self): # Ensure that write events don't come in while we're waiting for # a read and haven't asked for writeability. (the reverse is # difficult to test for) client, server = socket.socketpair() try: def handler(fd, events): self.assertEqual(events, IOLoop.READ) self.stop() self.io_loop.add_handler(client.fileno(), handler, IOLoop.READ) self.io_loop.add_timeout(self.io_loop.time() + 0.01, functools.partial(server.send, b'asdf')) self.wait() self.io_loop.remove_handler(client.fileno()) finally: client.close() server.close()
Example #4
Source File: ioloop_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
def test_read_while_writeable(self): # Ensure that write events don't come in while we're waiting for # a read and haven't asked for writeability. (the reverse is # difficult to test for) client, server = socket.socketpair() try: def handler(fd, events): self.assertEqual(events, IOLoop.READ) self.stop() self.io_loop.add_handler(client.fileno(), handler, IOLoop.READ) self.io_loop.add_timeout( self.io_loop.time() + 0.01, functools.partial(server.send, b"asdf") ) self.wait() self.io_loop.remove_handler(client.fileno()) finally: client.close() server.close()
Example #5
Source File: ioloop_test.py From tornado-zh with MIT License | 6 votes |
def test_read_while_writeable(self): # Ensure that write events don't come in while we're waiting for # a read and haven't asked for writeability. (the reverse is # difficult to test for) client, server = socket.socketpair() try: def handler(fd, events): self.assertEqual(events, IOLoop.READ) self.stop() self.io_loop.add_handler(client.fileno(), handler, IOLoop.READ) self.io_loop.add_timeout(self.io_loop.time() + 0.01, functools.partial(server.send, b'asdf')) self.wait() self.io_loop.remove_handler(client.fileno()) finally: client.close() server.close()
Example #6
Source File: ioloop_test.py From pySINDy with MIT License | 6 votes |
def test_read_while_writeable(self): # Ensure that write events don't come in while we're waiting for # a read and haven't asked for writeability. (the reverse is # difficult to test for) client, server = socket.socketpair() try: def handler(fd, events): self.assertEqual(events, IOLoop.READ) self.stop() self.io_loop.add_handler(client.fileno(), handler, IOLoop.READ) self.io_loop.add_timeout(self.io_loop.time() + 0.01, functools.partial(server.send, b'asdf')) self.wait() self.io_loop.remove_handler(client.fileno()) finally: client.close() server.close()
Example #7
Source File: connection.py From ironpython2 with Apache License 2.0 | 6 votes |
def Pipe(duplex=True): ''' Returns pair of connection objects at either end of a pipe ''' if duplex: s1, s2 = socket.socketpair() s1.setblocking(True) s2.setblocking(True) c1 = _multiprocessing.Connection(os.dup(s1.fileno())) c2 = _multiprocessing.Connection(os.dup(s2.fileno())) s1.close() s2.close() else: fd1, fd2 = os.pipe() c1 = _multiprocessing.Connection(fd1, writable=False) c2 = _multiprocessing.Connection(fd2, readable=False) return c1, c2
Example #8
Source File: test_daemon.py From Pyro5 with MIT License | 6 votes |
def testDaemonConnectedSocket(self): try: Pyro5.config.SERVERTYPE = "thread" with Pyro5.server.Daemon() as d: assert "Thread" in d.transportServer.__class__.__name__ s1, s2 = socket.socketpair() with Pyro5.server.Daemon(connected_socket=s1) as d: assert d.locationStr=="./u:<<not-bound>>" or d.locationStr.startswith("127.0.") assert not("Thread" in d.transportServer.__class__.__name__) assert "Existing" in d.transportServer.__class__.__name__ Pyro5.config.SERVERTYPE = "multiplex" with Pyro5.server.Daemon() as d: assert "Multiplex" in d.transportServer.__class__.__name__ s1, s2 = socket.socketpair() with Pyro5.server.Daemon(connected_socket=s1) as d: assert d.locationStr=="./u:<<not-bound>>" or d.locationStr.startswith("127.0.") assert not("Multiplex" in d.transportServer.__class__.__name__) assert "Existing" in d.transportServer.__class__.__name__ finally: Pyro5.config.SERVERTYPE = "thread"
Example #9
Source File: connection.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def Pipe(duplex=True): ''' Returns pair of connection objects at either end of a pipe ''' if duplex: s1, s2 = socket.socketpair() s1.setblocking(True) s2.setblocking(True) c1 = Connection(s1.detach()) c2 = Connection(s2.detach()) else: fd1, fd2 = os.pipe() c1 = Connection(fd1, writable=False) c2 = Connection(fd2, readable=False) return c1, c2
Example #10
Source File: test_kqueue.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_issue30058(self): # changelist must be an iterable kq = select.kqueue() a, b = socket.socketpair() ev = select.kevent(a, select.KQ_FILTER_READ, select.KQ_EV_ADD | select.KQ_EV_ENABLE) kq.control([ev], 0) # not a list kq.control((ev,), 0) # __len__ is not consistent with __iter__ class BadList: def __len__(self): return 0 def __iter__(self): for i in range(100): yield ev kq.control(BadList(), 0) # doesn't have __len__ kq.control(iter([ev]), 0) a.close() b.close() kq.close()
Example #11
Source File: test_selectors.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): with socket.socket(family, type, proto) as l: l.bind((support.HOST, 0)) l.listen() c = socket.socket(family, type, proto) try: c.connect(l.getsockname()) caddr = c.getsockname() while True: a, addr = l.accept() # check that we've got the correct client if addr == caddr: return c, a a.close() except OSError: c.close() raise
Example #12
Source File: TNonblockingServer.py From galaxy-sdk-python with Apache License 2.0 | 6 votes |
def __init__(self, processor, lsocket, inputProtocolFactory=None, outputProtocolFactory=None, threads=10): self.processor = processor self.socket = lsocket self.in_protocol = inputProtocolFactory or TBinaryProtocolFactory() self.out_protocol = outputProtocolFactory or self.in_protocol self.threads = int(threads) self.clients = {} self.tasks = Queue.Queue() self._read, self._write = socket.socketpair() self.prepared = False self._stop = False
Example #13
Source File: test_pty.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test__copy_eof_on_all(self): """Test the empty read EOF case on both master_fd and stdin.""" read_from_stdout_fd, mock_stdout_fd = self._pipe() pty.STDOUT_FILENO = mock_stdout_fd mock_stdin_fd, write_to_stdin_fd = self._pipe() pty.STDIN_FILENO = mock_stdin_fd socketpair = self._socketpair() masters = [s.fileno() for s in socketpair] os.close(masters[1]) socketpair[1].close() os.close(write_to_stdin_fd) # Expect two select calls, the last one will cause IndexError pty.select = self._mock_select self.select_rfds_lengths.append(2) self.select_rfds_results.append([mock_stdin_fd, masters[0]]) # We expect that both fds were removed from the fds list as they # both encountered an EOF before the second select call. self.select_rfds_lengths.append(0) with self.assertRaises(IndexError): pty._copy(masters[0])
Example #14
Source File: ioloop_test.py From viewfinder with Apache License 2.0 | 6 votes |
def test_read_while_writeable(self): # Ensure that write events don't come in while we're waiting for # a read and haven't asked for writeability. (the reverse is # difficult to test for) client, server = socket.socketpair() try: def handler(fd, events): self.assertEqual(events, IOLoop.READ) self.stop() self.io_loop.add_handler(client.fileno(), handler, IOLoop.READ) self.io_loop.add_timeout(self.io_loop.time() + 0.01, functools.partial(server.send, b'asdf')) self.wait() self.io_loop.remove_handler(client.fileno()) finally: client.close() server.close()
Example #15
Source File: TNonblockingServer.py From Protect4 with GNU General Public License v3.0 | 6 votes |
def __init__(self, processor, lsocket, inputProtocolFactory=None, outputProtocolFactory=None, threads=10): self.processor = processor self.socket = lsocket self.in_protocol = inputProtocolFactory or TBinaryProtocolFactory() self.out_protocol = outputProtocolFactory or self.in_protocol self.threads = int(threads) self.clients = {} self.tasks = queue.Queue() self._read, self._write = socket.socketpair() self.prepared = False self._stop = False
Example #16
Source File: ioloop_test.py From tornado-zh with MIT License | 6 votes |
def test_read_while_writeable(self): # Ensure that write events don't come in while we're waiting for # a read and haven't asked for writeability. (the reverse is # difficult to test for) client, server = socket.socketpair() try: def handler(fd, events): self.assertEqual(events, IOLoop.READ) self.stop() self.io_loop.add_handler(client.fileno(), handler, IOLoop.READ) self.io_loop.add_timeout(self.io_loop.time() + 0.01, functools.partial(server.send, b'asdf')) self.wait() self.io_loop.remove_handler(client.fileno()) finally: client.close() server.close()
Example #17
Source File: test_client_destroy.py From pywayland with Apache License 2.0 | 6 votes |
def test_client_destroy_listener(): global a, b s1, s2 = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0) a = 0 b = 0 display = Display() client = Client(display, s1.fileno()) destroy_listener_a = Listener(destroy_notify_a) destroy_listener_b = Listener(destroy_notify_b) client.add_destroy_listener(destroy_listener_a) client.add_destroy_listener(destroy_listener_b) destroy_listener_a.remove() client.destroy() assert a == 0 assert b == 1
Example #18
Source File: test_resource.py From pywayland with Apache License 2.0 | 6 votes |
def test_create_resource(): s1, s2 = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0) display = Display() client = Client(display, s1.fileno()) # Create resource res = WlDisplay.resource_class(client, version=4) assert res.version == 4 # Fetching the client object by id gives the resource back again assert client.get_object(res.id) == res client.user_data = 0xbee assert client.user_data == 0xbee client.destroy() display.destroy() s2.close()
Example #19
Source File: connection.py From oss-ftp with MIT License | 6 votes |
def Pipe(duplex=True): ''' Returns pair of connection objects at either end of a pipe ''' if duplex: s1, s2 = socket.socketpair() s1.setblocking(True) s2.setblocking(True) c1 = _multiprocessing.Connection(os.dup(s1.fileno())) c2 = _multiprocessing.Connection(os.dup(s2.fileno())) s1.close() s2.close() else: fd1, fd2 = os.pipe() c1 = _multiprocessing.Connection(fd1, writable=False) c2 = _multiprocessing.Connection(fd2, readable=False) return c1, c2
Example #20
Source File: test_pty.py From oss-ftp with MIT License | 6 votes |
def test__copy_eof_on_all(self): """Test the empty read EOF case on both master_fd and stdin.""" read_from_stdout_fd, mock_stdout_fd = self._pipe() pty.STDOUT_FILENO = mock_stdout_fd mock_stdin_fd, write_to_stdin_fd = self._pipe() pty.STDIN_FILENO = mock_stdin_fd socketpair = socket.socketpair() masters = [s.fileno() for s in socketpair] self.fds.extend(masters) os.close(masters[1]) socketpair[1].close() os.close(write_to_stdin_fd) # Expect two select calls, the last one will cause IndexError pty.select = self._mock_select self.select_rfds_lengths.append(2) self.select_rfds_results.append([mock_stdin_fd, masters[0]]) # We expect that both fds were removed from the fds list as they # both encountered an EOF before the second select call. self.select_rfds_lengths.append(0) with self.assertRaises(IndexError): pty._copy(masters[0])
Example #21
Source File: test_resource.py From pywayland with Apache License 2.0 | 6 votes |
def notest_create_resource_with_same_id(): s1, s2 = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0) display = Display() client = Client(display, s1.fileno()) # Create resource res = WlDisplay.resource_class(client, version=2) assert client.get_object(res.id) == res # This should replace the old one res2 = WlDisplay.resource_class(client, version=1, id=res.id) assert client.get_object(res.id) == res2 res2.destroy() res.destroy() client.destroy() display.destroy() s2.close()
Example #22
Source File: unix_events.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs): stdin_w = None if stdin == subprocess.PIPE: # Use a socket pair for stdin, since not all platforms # support selecting read events on the write end of a # socket (which we use in order to detect closing of the # other end). Notably this is needed on AIX, and works # just fine on other platforms. stdin, stdin_w = self._loop._socketpair() # Mark the write end of the stdin pipe as non-inheritable, # needed by close_fds=False on Python 3.3 and older # (Python 3.4 implements the PEP 446, socketpair returns # non-inheritable sockets) _set_inheritable(stdin_w.fileno(), False) self._proc = subprocess.Popen( args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr, universal_newlines=False, bufsize=bufsize, **kwargs) if stdin_w is not None: stdin.close() self._proc.stdin = open(stdin_w.detach(), 'wb', buffering=bufsize)
Example #23
Source File: test_ioloop.py From oss-ftp with MIT License | 6 votes |
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): with contextlib.closing(socket.socket(family, type, proto)) as l: l.bind(("localhost", 0)) l.listen() c = socket.socket(family, type, proto) try: c.connect(l.getsockname()) caddr = c.getsockname() while True: a, addr = l.accept() # check that we've got the correct client if addr == caddr: return c, a a.close() except OSError: c.close() raise # TODO: write more tests.
Example #24
Source File: test_ioloop.py From oss-ftp with MIT License | 6 votes |
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): with contextlib.closing(socket.socket(family, type, proto)) as l: l.bind(("localhost", 0)) l.listen() c = socket.socket(family, type, proto) try: c.connect(l.getsockname()) caddr = c.getsockname() while True: a, addr = l.accept() # check that we've got the correct client if addr == caddr: return c, a a.close() except OSError: c.close() raise # TODO: write more tests.
Example #25
Source File: ioloop_test.py From viewfinder with Apache License 2.0 | 6 votes |
def test_read_while_writeable(self): # Ensure that write events don't come in while we're waiting for # a read and haven't asked for writeability. (the reverse is # difficult to test for) client, server = socket.socketpair() try: def handler(fd, events): self.assertEqual(events, IOLoop.READ) self.stop() self.io_loop.add_handler(client.fileno(), handler, IOLoop.READ) self.io_loop.add_timeout(self.io_loop.time() + 0.01, functools.partial(server.send, b'asdf')) self.wait() self.io_loop.remove_handler(client.fileno()) finally: client.close() server.close()
Example #26
Source File: test_sendmsg.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def setUp(self): """ Create a pair of UNIX sockets. """ self.input, self.output = socketpair(AF_UNIX)
Example #27
Source File: TNonblockingServer.py From Protect4 with GNU General Public License v3.0 | 5 votes |
def wake_up(self): """Wake up main thread. The server usually waits in select call in we should terminate one. The simplest way is using socketpair. Select always wait to read from the first socket of socketpair. In this case, we can just write anything to the second socket from socketpair. """ self._write.send(b'1')
Example #28
Source File: test_selectors.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def make_socketpair(self): rd, wr = socketpair() self.addCleanup(rd.close) self.addCleanup(wr.close) return rd, wr
Example #29
Source File: server.py From etesync-dav with GNU General Public License v3.0 | 5 votes |
def get_request(self): # Set timeout for client request, client_address = super().get_request() timeout = self.configuration.get("server", "timeout") if timeout: request.settimeout(timeout) client_socket, client_socket_out = socket.socketpair() self.client_sockets.add(client_socket_out) return request, (*client_address, client_socket)
Example #30
Source File: automaton.py From CVE-2016-6366 with MIT License | 5 votes |
def __init__(self, name, ioevent, automaton, proto, args, kargs): self.name = name self.ioevent = ioevent self.proto = proto self.spa,self.spb = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM) kargs["external_fd"] = {ioevent:self.spb} self.atmt = automaton(*args, **kargs) self.atmt.runbg()