Skip to main content

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

Release files for pystun3 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pystun3 2.0.0
File Size Uploaded
pystun3-2.0.0.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pystun3 2.0.0
File Interpreter ABI Platform
pystun3-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / pystun3-2.0.0.tar.gz

Download URL pystun3-2.0.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
99abfe83df69fd0f6278e9be1456827fafb9e4e25efed8074d15b754ac9b1fee
BLAKE2b-256 checksum
How to use checksums
3100a154d38357e2135d96bd633f10159d91f97feebdf54273a9d2e4e92dcc08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release files / pystun3-2.0.0-py3-none-any.whl

Download URL pystun3-2.0.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4824c72d2c8c0297df92dbdf7cc57b346d6cf0de4e5a428c3be12d28a8bce0f3
BLAKE2b-256 checksum
How to use checksums
c822dd2f0a266f88d43bfbf619fd3189d22e9248f7bad8d0b14bd34e57c45f54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page