def test_tcp(self): addr = ("127.0.0.1", get_free_port()) assert not thisproc.connections(kind='tcp4') server, client = tcp_socketpair(AF_INET, addr=addr) with nested(closing(server), closing(client)): cons = thisproc.connections(kind='tcp4') self.assertEqual(len(cons), 2) self.assertEqual(cons[0].status, psutil.CONN_ESTABLISHED) self.assertEqual(cons[1].status, psutil.CONN_ESTABLISHED)
def tcp_tcp_socketpair(self): addr = ("127.0.0.1", get_free_port()) server, client = tcp_socketpair(socket.AF_INET, addr=addr) with contextlib.closing(server): with contextlib.closing(client): # Ensure they are connected and the positions are # correct. self.assertEqual(server.getsockname(), addr) self.assertEqual(client.getpeername(), addr) self.assertNotEqual(client.getsockname(), addr)
def test_tcp(self): addr = ("127.0.0.1", get_free_port()) server, client = tcp_socketpair(AF_INET, addr=addr) with nested(closing(server), closing(client)): cons = psutil.Process().connections(kind='all') server_conn, client_conn = self.distinguish_tcp_socks(cons, addr) self.check_socket(server, conn=server_conn) self.check_socket(client, conn=client_conn) self.assertEqual(server_conn.status, psutil.CONN_ESTABLISHED) self.assertEqual(client_conn.status, psutil.CONN_ESTABLISHED)
def test_tcp(self): addr = ("127.0.0.1", get_free_port()) assert not thisproc.connections(kind='tcp4') server, client = tcp_socketpair(AF_INET, addr=addr) try: cons = thisproc.connections(kind='tcp4') self.assertEqual(len(cons), 2) self.assertEqual(cons[0].status, psutil.CONN_ESTABLISHED) self.assertEqual(cons[1].status, psutil.CONN_ESTABLISHED) # May not be fast enough to change state so it stays # commenteed. # client.close() # cons = thisproc.connections(kind='all') # self.assertEqual(len(cons), 1) # self.assertEqual(cons[0].status, psutil.CONN_CLOSE_WAIT) finally: server.close() client.close()
def test_udp_v6(self): addr = ("::1", get_free_port()) with closing(bind_socket(AF_INET6, SOCK_DGRAM, addr=addr)) as sock: conn = self.check_socket(sock) assert not conn.raddr self.assertEqual(conn.status, psutil.CONN_NONE)
def test_tcp_v4(self): addr = ("127.0.0.1", get_free_port()) with closing(bind_socket(AF_INET, SOCK_STREAM, addr=addr)) as sock: conn = self.check_socket(sock) assert not conn.raddr self.assertEqual(conn.status, psutil.CONN_LISTEN)
def bind_socket(self): port = get_free_port() with contextlib.closing(bind_socket(addr=('', port))) as s: self.assertEqual(s.getsockname()[1], port)
def test_udp_v4(self): addr = ("127.0.0.1", get_free_port()) with closing(bind_socket(addr, AF_INET, SOCK_DGRAM)) as sock: conn = self.check_socket(sock) assert not conn.raddr self.assertEqual(conn.status, psutil.CONN_NONE)
def test_tcp_v6(self): addr = ("::1", get_free_port()) with closing(bind_socket(addr, AF_INET6, SOCK_STREAM)) as sock: conn = self.check_socket(sock) assert not conn.raddr self.assertEqual(conn.status, psutil.CONN_LISTEN)