random-port 0.0.1__py3-none-any.whl → 0.0.3__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.

Potentially problematic release.


This version of random-port might be problematic. Click here for more details.

random_port/core.py ADDED
@@ -0,0 +1,76 @@
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 RandomPort(ABC):
28
+ @abstractmethod
29
+ def value(self) -> int:
30
+ pass
31
+
32
+
33
+ class TcpRandomPort(RandomPort):
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(RandomPort):
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: random_port
3
- Version: 0.0.1
3
+ Version: 0.0.3
4
4
  Summary: Generate a free TCP or UDP random port
5
5
  Author-email: Fabrício Barros Cabral <fabriciofx@gmail.com>
6
6
  License-Expression: MIT
@@ -34,16 +34,20 @@ in your project folder.
34
34
 
35
35
  ## Usage
36
36
 
37
- - To a TCP random port:
37
+ - To use a TCP random port:
38
38
 
39
39
  ```python
40
- port = RandomTcpPort().value()
40
+ from random_port.core import TcpRandomPort
41
+
42
+ port = TcpRandomPort().value()
41
43
  ```
42
44
 
43
- - To a UDP random port:
45
+ - To use a UDP random port:
44
46
 
45
47
  ```python
46
- port = RandomUdpPort().value()
48
+ from random_port.core import UdpRandomPort
49
+
50
+ port = UdpRandomPort().value()
47
51
  ```
48
52
 
49
53
  ## License
@@ -0,0 +1,7 @@
1
+ random_port/__init__.py,sha256=WnbD7VqLlKdJ81-K8BGjxrmQLXEu_g-czd-Y73OAezQ,1129
2
+ random_port/core.py,sha256=uzh051hXfHpcZalg6HaoxDIdGgU9ttXzZ35BU2eTL1g,2756
3
+ random_port-0.0.3.dist-info/licenses/LICENSE.txt,sha256=SrFTda5sgSB9hkS5IK7GpSHxGMawR9F_6099TboKYXk,1091
4
+ random_port-0.0.3.dist-info/METADATA,sha256=QK-9jJNyC_0LC-pF57oO0yVF_w5p8P71Mhmdr7m9xOc,2248
5
+ random_port-0.0.3.dist-info/WHEEL,sha256=1tXe9gY0PYatrMPMDd6jXqjfpz_B-Wqm32CPfRC58XU,91
6
+ random_port-0.0.3.dist-info/top_level.txt,sha256=wypEHVVFuk5I-FKJUMe84GL6xu7FPBEa8iRY_oyqxfw,12
7
+ random_port-0.0.3.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- random_port/__init__.py,sha256=WnbD7VqLlKdJ81-K8BGjxrmQLXEu_g-czd-Y73OAezQ,1129
2
- random_port-0.0.1.dist-info/licenses/LICENSE.txt,sha256=SrFTda5sgSB9hkS5IK7GpSHxGMawR9F_6099TboKYXk,1091
3
- random_port-0.0.1.dist-info/METADATA,sha256=pSEeBfhD9kvWkWxP-lg-keH9BTGWchulFpgZ1PpOOiQ,2152
4
- random_port-0.0.1.dist-info/WHEEL,sha256=1tXe9gY0PYatrMPMDd6jXqjfpz_B-Wqm32CPfRC58XU,91
5
- random_port-0.0.1.dist-info/top_level.txt,sha256=wypEHVVFuk5I-FKJUMe84GL6xu7FPBEa8iRY_oyqxfw,12
6
- random_port-0.0.1.dist-info/RECORD,,