random-port 0.0.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- random_port/__init__.py +21 -0
- random_port/pool.py +114 -0
- random_port/py.typed +0 -0
- random_port-0.0.8.dist-info/METADATA +89 -0
- random_port-0.0.8.dist-info/RECORD +8 -0
- random_port-0.0.8.dist-info/WHEEL +5 -0
- random_port-0.0.8.dist-info/licenses/LICENSE.txt +21 -0
- random_port-0.0.8.dist-info/top_level.txt +1 -0
random_port/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# The MIT License (MIT)
|
|
2
|
+
#
|
|
3
|
+
# Copyright (C) 2025 Fabrício Barros Cabral
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included
|
|
13
|
+
# in all copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
# SOFTWARE.
|
random_port/pool.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# The MIT License (MIT)
|
|
2
|
+
#
|
|
3
|
+
# Copyright (C) 2025 Fabrício Barros Cabral
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included
|
|
13
|
+
# in all copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
# SOFTWARE.
|
|
22
|
+
import random
|
|
23
|
+
import socket
|
|
24
|
+
from abc import ABC, abstractmethod
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Port(ABC):
|
|
28
|
+
@abstractmethod
|
|
29
|
+
def value(self) -> int:
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class TcpRandomPort(Port):
|
|
34
|
+
def __init__(
|
|
35
|
+
self, host: str = "127.0.0.1", begin: int = 1024, end: int = 65536
|
|
36
|
+
) -> None:
|
|
37
|
+
self.__host = host
|
|
38
|
+
self.__begin = begin
|
|
39
|
+
self.__end = end
|
|
40
|
+
|
|
41
|
+
def value(self) -> int:
|
|
42
|
+
def is_tcp_port_in_use(host: str, port: int) -> bool:
|
|
43
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sckt:
|
|
44
|
+
try:
|
|
45
|
+
sckt.bind((host, port))
|
|
46
|
+
except OSError:
|
|
47
|
+
return True
|
|
48
|
+
return False
|
|
49
|
+
|
|
50
|
+
port = random.randint(self.__begin, self.__end)
|
|
51
|
+
while is_tcp_port_in_use(self.__host, port):
|
|
52
|
+
port = random.randint(self.__begin, self.__end)
|
|
53
|
+
return port
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class UdpRandomPort(Port):
|
|
57
|
+
def __init__(
|
|
58
|
+
self, host: str = "127.0.0.1", begin: int = 1024, end: int = 65536
|
|
59
|
+
) -> None:
|
|
60
|
+
self.__host = host
|
|
61
|
+
self.__begin = begin
|
|
62
|
+
self.__end = end
|
|
63
|
+
|
|
64
|
+
def value(self) -> int:
|
|
65
|
+
def is_udp_port_in_use(host: str, port: int) -> bool:
|
|
66
|
+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sckt:
|
|
67
|
+
try:
|
|
68
|
+
sckt.bind((host, port))
|
|
69
|
+
except OSError:
|
|
70
|
+
return True
|
|
71
|
+
return False
|
|
72
|
+
|
|
73
|
+
port = random.randint(self.__begin, self.__end)
|
|
74
|
+
while is_udp_port_in_use(self.__host, port):
|
|
75
|
+
port = random.randint(self.__begin, self.__end)
|
|
76
|
+
return port
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class TcpPort(Port):
|
|
80
|
+
def __init__(self, num: int, host: str = "127.0.0.1") -> None:
|
|
81
|
+
self.__host = host
|
|
82
|
+
self.__num = num
|
|
83
|
+
|
|
84
|
+
def value(self) -> int:
|
|
85
|
+
def is_tcp_port_in_use(host: str, port: int) -> bool:
|
|
86
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sckt:
|
|
87
|
+
try:
|
|
88
|
+
sckt.bind((host, port))
|
|
89
|
+
except OSError:
|
|
90
|
+
return True
|
|
91
|
+
return False
|
|
92
|
+
|
|
93
|
+
while is_tcp_port_in_use(self.__host, self.__num):
|
|
94
|
+
pass
|
|
95
|
+
return self.__num
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class UdpPort(Port):
|
|
99
|
+
def __init__(self, num: int, host: str = "127.0.0.1") -> None:
|
|
100
|
+
self.__host = host
|
|
101
|
+
self.__num = num
|
|
102
|
+
|
|
103
|
+
def value(self) -> int:
|
|
104
|
+
def is_udp_port_in_use(host: str, port: int) -> bool:
|
|
105
|
+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sckt:
|
|
106
|
+
try:
|
|
107
|
+
sckt.bind((host, port))
|
|
108
|
+
except OSError:
|
|
109
|
+
return True
|
|
110
|
+
return False
|
|
111
|
+
|
|
112
|
+
while is_udp_port_in_use(self.__host, self.__num):
|
|
113
|
+
pass
|
|
114
|
+
return self.__num
|
random_port/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: random-port
|
|
3
|
+
Version: 0.0.8
|
|
4
|
+
Summary: Generate a free TCP or UDP random port
|
|
5
|
+
Author-email: Fabrício Barros Cabral <fabriciofx@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/fabriciofx/random-port
|
|
8
|
+
Project-URL: bug-tracker, https://github.com/fabriciofx/random-port/issues
|
|
9
|
+
Keywords: socket,tcp,udp,random,port,test
|
|
10
|
+
Requires-Python: >=3.14
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE.txt
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# Random Port
|
|
16
|
+
|
|
17
|
+
## Introduction
|
|
18
|
+
|
|
19
|
+
Usually we need start a TCP or UDP server (mainly to use in tests) but, which
|
|
20
|
+
port must be used? So, random-port is the answer. It generate a free random
|
|
21
|
+
or fixed TCP/UDP port to be used for any server.
|
|
22
|
+
|
|
23
|
+
## How to install
|
|
24
|
+
|
|
25
|
+
Install it using `pip` command:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install random-port
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
in your project folder.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
- To use a TCP random port:
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from random_port.pool import TcpRandomPort
|
|
39
|
+
|
|
40
|
+
port = TcpRandomPort().value()
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
- To use a random UDP port:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from random_port.pool import UdpRandomPort
|
|
47
|
+
|
|
48
|
+
port = UdpRandomPort().value()
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
- To use a fixed TCP port:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from random_port.pool import TcpPort
|
|
55
|
+
|
|
56
|
+
port = TcpPort(12345).value()
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
- To use a fixed UDP port:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from random_port.pool import UdpPort
|
|
63
|
+
|
|
64
|
+
port = UdpPort(12345).value()
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
The MIT License (MIT)
|
|
70
|
+
|
|
71
|
+
Copyright (C) 2025 Fabrício Barros Cabral
|
|
72
|
+
|
|
73
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
74
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
75
|
+
in the Software without restriction, including without limitation the rights
|
|
76
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
77
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
78
|
+
furnished to do so, subject to the following conditions:
|
|
79
|
+
|
|
80
|
+
The above copyright notice and this permission notice shall be included
|
|
81
|
+
in all copies or substantial portions of the Software.
|
|
82
|
+
|
|
83
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
84
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
85
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
86
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
87
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
88
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
89
|
+
SOFTWARE.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
random_port/__init__.py,sha256=WnbD7VqLlKdJ81-K8BGjxrmQLXEu_g-czd-Y73OAezQ,1129
|
|
2
|
+
random_port/pool.py,sha256=HhiqgogxNnX8XjXM_LdC5wgzeMXyRQfPg3T-KSQE0-o,3877
|
|
3
|
+
random_port/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
random_port-0.0.8.dist-info/licenses/LICENSE.txt,sha256=SrFTda5sgSB9hkS5IK7GpSHxGMawR9F_6099TboKYXk,1091
|
|
5
|
+
random_port-0.0.8.dist-info/METADATA,sha256=TntSVhSW-dpWmXuSTrEcqnGKWk-c0O7zuP-VXDwkY1A,2406
|
|
6
|
+
random_port-0.0.8.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
7
|
+
random_port-0.0.8.dist-info/top_level.txt,sha256=wypEHVVFuk5I-FKJUMe84GL6xu7FPBEa8iRY_oyqxfw,12
|
|
8
|
+
random_port-0.0.8.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2025 Fabrício Barros Cabral
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included
|
|
13
|
+
in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
random_port
|