Skip to content

Python

Tester la connexion à un port TCP (équivalent telnet)

import socket

def test_tcp_connection(host, port):
    try:
        with socket.create_connection((host, port), timeout=10) as sock:
            print(f"Connection to {host}:{port} successful")
            return True
    except (socket.timeout, socket.error) as e:
        print(f"Connection to {host}:{port} failed: {e}")
        return False

# Example usage
if __name__ == "__main__":
    redishosts = ["8.8.8.8"]
    port = 10110
    for host in redishosts:
        test_tcp_connection(host, port)

Last update: February 11, 2025
Created: February 11, 2025