Skip to main content

A Python STUN client for getting NAT type and external IP (RFC 3489)

Project description

A Python STUN client for getting NAT type and external IP. Supports Python versions 2 and 3.

This project has been forked several times:

PyStun follows RFC 3489. A server following STUN-bis hasn’t been found on internet so RFC3489 is the only implementation.

Installation

To install the latest version:

$ pip install pystun3

or download/clone the source and install manually using:

$ cd /path/to/pystun3/src
$ python setup.py install

If you’re hacking on pystun3 you should use the ‘develop’ command instead:

$ python setup.py develop

This will make a link to the sources inside your site-packages directory so that any changes are immediately available for testing.

Usage

From command line:

$ pystun3
NAT Type: Full Cone
External IP: <your-ip-here>
External Port: 54320

Pass –help for more options:

% pystun3 --help
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

From Python:

import stun
nat_type, external_ip, external_port = stun.get_ip_info()

This will rotate through an internal list of STUN servers until a response is found. If no response is found you will get "Blocked" as the nat_type and None for external_ip and external_port.

If you prefer to use a specific STUN server:

nat_type, external_ip, external_port = stun.get_ip_info(stun_host='stun.ekiga.net')

If you prefer to use a specific STUN server port:

nat_type, external_ip, external_port = stun.get_ip_info(stun_port=3478)

You may also specify the client interface and port that is used although this is not needed:

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)

Read the code for more details…

LICENSE

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystun3-fix-1.0.1.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file pystun3-fix-1.0.1.tar.gz.

File metadata

  • Download URL: pystun3-fix-1.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.8

File hashes

Hashes for pystun3-fix-1.0.1.tar.gz
Algorithm Hash digest
SHA256 99622181d4a2c66115fa60f7b43053c6ad02725c34051e231edb9e477ebc3c43
MD5 d849b37324aab19bc8449ffd7077f8bb
BLAKE2b-256 61e9c2158456fba87effa41047a4e58c283da0582c126dece92fa3d164a98152

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page