V0.2
V0.2 与 V0.1 的区别:
仅此一处区别:
ports = port.auto_ports(50)
V0.1 会随机选择 50 个端口号检测,返回可用的端口号列表。因此返回的端口数为 0 到 50 个。
V0.2 会返回含有 50 个端口的列表,不多不少恰好 50 个。
当然,这里的 50 是一个用户输入的任意整数。
# -*- coding: utf-8 -*-
import port
# 从命令行获取输入的端口
flask_port = port.input_port('HTTP 端口:')
# 检测端口是否被占用
if port.port_is_used(80):
print('端口已被占用!')
else:
print('端口可用!')
# 自动获取 50 个可用的随机端口(使用多线程检测),返回端口列表
ports = port.auto_ports(50)
print(ports)
V0.1
使用示例代码:
# -*- coding: utf-8 -*-
import port
# 从命令行获取输入的端口
flask_port = port.input_port('HTTP 端口:')
# 检测端口是否被占用
if port.port_is_used(80):
print('端口已被占用!')
else:
print('端口可用!')
# 自动检测 50 个随机端口(使用多线程检测),返回可用的端口列表
ports = port.auto_ports(50)
print(ports)
输出:
$ python port_demo.py
HTTP 端口:8889
端口已被占用,请重新输入!
HTTP 端口:wet
只能输入 int 类型,请重新输入!
HTTP 端口:80
端口可用!
[27250, 32020, 30968, 35608, 20766, 30209, 37685, 6318, 10303, 17774, 37215, 9358, 30471, 33978, 11569, 21199, 17627, 15395, 26965, 21741, 40834, 32150, 8296, 43340, 41871, 37729, 29003, 44646, 17823, 49076, 16418, 45504, 2707, 10403, 24558, 34549, 30435, 19741, 5965, 31398, 39324, 39346, 48115, 5050, 38273, 21023, 21666, 21976, 25894, 11659]
下载: