8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
10 # FOR A PARTICULAR PURPOSE. |
10 # FOR A PARTICULAR PURPOSE. |
11 # |
11 # |
12 |
12 |
13 __docformat__ = 'restructuredtext' |
13 """PyAMS_utils.protocol.tcp module |
|
14 |
|
15 This module only provides a single function, used to know if a given TCP port is already in use |
|
16 """ |
14 |
17 |
15 import socket |
18 import socket |
|
19 |
|
20 __docformat__ = 'restructuredtext' |
16 |
21 |
17 |
22 |
18 def is_port_in_use(port, hostname='localhost'): |
23 def is_port_in_use(port, hostname='localhost'): |
19 """Check if given port is already used locally""" |
24 """Check if given port is already used locally""" |
20 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
25 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
21 return s.connect_ex((hostname, port)) == 0 |
26 return sock.connect_ex((hostname, port)) == 0 |