python3 获取nat类型和外部ip PyStun3 PyNAT 库简介
2021/10/26 17:11:59
本文主要是介绍python3 获取nat类型和外部ip PyStun3 PyNAT 库简介,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
PyStun3 库简介
Installation 安装
Usage 用法
PyNAT 库简介
Installation 安装
From PyPI 来自 PyPI
From GitHub 来自 GitHub
Usage 用法
PyStun3 库简介
官网地址:官网地址:https://github.com/talkiq/pystun3
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:
如果你正在使用 pystun3,你应该使用‘ develop’命令:
$ 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:
来自 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
.
这将通过 STUN 服务器的内部列表循环,直到找到响应。如果没有找到响应,您将得到“ Blocked”作为 nat _ type,而 None 作为 external _ ip 和 external _ port。
If you prefer to use a specific STUN server:
如果您喜欢使用特定的 STUN 服务器:
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:
如果您喜欢使用特定的 STUN 服务器端口:
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)
PyNAT 库简介
官网地址:https://github.com/aarant/pynat
Discover external IP addresses and NAT topologies using STUN (Simple Traversal of UDP Through Network Address Translators).
使用 STUN (通过网络地址转换器的 UDP 简单遍历)发现外部 IP 地址和 NAT 拓扑。
PyNAT follows RFC 3489, and is inspired by a similar program for Python 2.x called PyStun. PyNAT supports Python 2.7 and later.
遵循 RFC 3489,灵感来自 Python 2. x 的一个类似程序 PyStun。支持 Python 2.7及更高版本。
Installation 安装
PyNAT requires Python 2.7 or later.
需要 Python 2.7或更高版本。
From PyPI 来自 PyPI
Install PyNAT by running pip3 install pynat
from the command line.
通过从命令行运行 pip3 Install PyNAT 来安装 PyNAT。
Note
注意
On some Linux systems, installation may require running pip with root permissions, or running pip3 install pynat --user
. The latter may require exporting ~/.local/bin to PATH.
在某些 Linux 系统上,安装可能需要使用 root 权限运行 pip,或者运行 pip3 install pynat -- user。后者可能需要出口。本地/垃圾桶到 PATH。
From GitHub 来自 GitHub
Clone or download the git repo, navigate to the directory, and run:
克隆或下载 git repo,导航到目录,然后运行:
python3 setup.py sdist cd dist pip3 install pynat-<version>.tar.gz
Usage 用法
To get information about the network topology and external IP/port used, run pynat
:
要获取有关网络拓扑和所使用的外部 IP/端口的信息,运行 pynat:
Network type: UDP Firewall Internal address: 127.0.0.1:54320 External address: 127.0.0.1:54320
Run pynat -h
or pynat --help
for more options:
运行 pynat-h 或 pynat-help for more options:
usage: pynat [-h] [--source_ip SOURCE_IP] [--source-port SOURCE_PORT] [--stun-host STUN_HOST] [--stun-port STUN_PORT] PyNAT v0.0.0 Discover external IP addresses and NAT topologies using STUN. Copyright (C) 2018 Ariel Antonitis. Licensed under the MIT License. optional arguments: -h, --help show this help message and exit --source_ip SOURCE_IP The source IPv4 address to bind to. --source-port SOURCE_PORT The source port to bind to. --stun-host STUN_HOST The STUN host to use for queries. --stun-port STUN_PORT The port of the STUN host to use for queries.
To use PyNAT inside a Python shell or project:
在 Python shell 或者项目中使用 PyNAT:
from pynat import get_ip_info topology, ext_ip, ext_port = get_ip_info()
To also get information about the internal IP, if unknown:
还可以获取内部 IP 的信息,如果不知道:
topology, ext_ip, ext_port, int_ip = get_ip_info(include_internal=True)
这篇关于python3 获取nat类型和外部ip PyStun3 PyNAT 库简介的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享