PyStun3

repository·master·Indexed 41 days ago

https://github.com/talkiq/pystun3

A Python STUN client following the RFC 3489 standard used to determine a device's NAT type and external IP address and port. It provides both a command-line utility and a Python API via stun.get_ip_info().

Tokens
1.3K
Snippets
7
Records
7
Agent score
2%

What's inside PyStun3

  1. Install PyStun3

    master

    You can install PyStun3 using pip or by installing from the source code. If you are developing the library, use the develop command to ensure changes are immediately available in your site-packages.

    # Install via pip
    $ pip install pystun3
    
    # Or install manually from source
    $ cd /path/to/pystun3/src
    $ python setup.py install
    
    # For development mode
    $ python setup.py develop
  2. Get IP information using stun.get_ip_info()

    master

    In Python, use stun.get_ip_info() to retrieve NAT information. The function rotates through an internal list of STUN servers until a response is found.

    Return Values: Returns a tuple of (nat_type, external_ip, external_port).

    Error Handling: If no STUN server responds, the function returns "Blocked" for nat_type and None for both external_ip and external_port.

    import stun
    nat_type, external_ip, external_port = stun.get_ip_info()
  3. Configure stun.get_ip_info() with custom servers and interfaces

    master

    You can customize the stun.get_ip_info() call by providing specific STUN hosts, ports, or local network interfaces.

    import stun
    
    # Use a specific STUN host
    nat_type, external_ip, external_port = stun.get_ip_info(stun_host='stun.ekiga.net')
    
    # Use a specific STUN port
    nat_type, external_ip, external_port = stun.get_ip_info(stun_port=3478)
    
    # Specify client interface (sip) and port
    sip = "0.0.0.0" # interface to listen on (all)
    port = 54320    # port to listen on
    nat_type, external_ip, external_port = stun.get_ip_info(sip, port)
  4. PyStun3 CLI reference

    master

    The following command-line arguments are available for the pystun3 utility:

    usage: pystun3 [-h] [-d] [-H STUN_HOST] [-P STUN_PORT] [-i SOURCE_IP] [-p SOURCE_PORT] [--version]
    
    optional arguments:
      -h, --help            show this help message and exit
      -d, --debug           Enable debug logging (default: False)
      -H STUN_HOST, --host STUN_HOST
                            STUN host to use (default: None)
      -P STUN_PORT, --host-port STUN_PORT
                            STUN host port to use (default: 3478)
      -i SOURCE_IP, --interface SOURCE_IP
                            network interface for client (default: 0.0.0.0)
      -p SOURCE_PORT, --port SOURCE_PORT
                            port to listen on for client (default: 54320)
      --version             show program's version number and exit
  5. Use PyStun3 from the command line

    master

    Run the pystun3 command to detect your NAT type, external IP, and external port. You can customize the behavior using various flags.

    $ pystun3
    NAT Type: Full Cone
    External IP: <your-ip-here>
    External Port: 54320
  6. Reference the pystun3 CLI flags and options

    master

    The following flags are available when running the pystun3 command-line tool:

    -d, --debug
        Enable debug logging
    -H, --stun-host
        STUN host to use
    -P, --stun-port
        STUN host port to use (integer)
    -i, --source-ip
        network interface for client
    -p, --source-port
        port to listen on for client
    --version
        Show the version
  7. Use the pystun3 CLI to check NAT type and external IP

    master

    The pystun3 command-line tool allows you to discover your NAT type, external IP address, and external port by communicating with a STUN server. By default, it uses predefined STUN host and port settings. You can override these settings using specific flags to test different network interfaces or STUN servers.

    # Basic usage
    pystun3
    
    # Specify a custom STUN server and network interface
    pystun3 --stun-host stun.l.google.com --source-ip 192.168.1.5
    
    # Enable debug logging to see the STUN exchange
    pystun3 --debug