-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
38 lines (31 loc) · 936 Bytes
/
Copy pathutil.py
File metadata and controls
38 lines (31 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Utility functions and constructs
"""
import hashlib
import logging
import sys
import socket, errno
from contextlib import closing
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logger = logging.getLogger(__name__)
def hashAlgo(algoCode, hashmethod='SHA256'):
logger.info(algoCode)
algoCode = algoCode.encode('utf-8')
h = hashlib.new(hashmethod)
h.update(algoCode)
return h.hexdigest()
def getFreePort():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(('', 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]
def isFreePort(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(('', port))
except socket.error as e:
if e.errno == errno.EADDRINUSE:
s.close()
return False
else:
s.close()
return True