-
Notifications
You must be signed in to change notification settings - Fork 0
/
iputils.py
40 lines (34 loc) · 1.17 KB
/
iputils.py
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
39
40
from socket import SOL_SOCKET, SOCK_STREAM, SO_REUSEADDR, socket, AF_INET, SOCK_DGRAM
from contextlib import closing
from requests import packages, Session
from requests.adapters import HTTPAdapter
def getMyIpAddr():
s = socket(AF_INET, SOCK_DGRAM)
try:
# google DNS should be ping-able
s.connect(("8.8.8.8", 1))
IP = s.getsockname()[0]
except OSError:
IP = "127.0.0.1"
finally:
s.close()
return IP
def findFreePort():
with closing(socket(AF_INET, SOCK_STREAM)) as s:
s.bind(('', 0))
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
return s.getsockname()[1]
# This creates a session request that will retry with backoff timing.
def requestsRetrySession(retries=1, backoff_factor=0.3, status_forcelist=(500, 502, 504), session=None):
session = session or Session()
retry = packages.urllib3.util.retry.Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount("http://", adapter)
session.mount("https://", adapter)
return session