forked from openatx/atxserver2-ios-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
42 lines (31 loc) · 865 Bytes
/
utils.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
41
42
# coding: utf-8
#
import collections
import random
import re
import socket
import string
def current_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
def update_recursive(d: dict, u: dict) -> dict:
for k, v in u.items():
if isinstance(v, collections.Mapping):
d[k] = update_recursive(d.get(k) or {}, v)
else:
d[k] = v
return d
def fix_url(url, scheme=None):
if not re.match(r"^(http|ws)s?://", url):
url = "http://" + url
if scheme:
url = re.compile(r"^http").sub(scheme, url)
return url
def id_generator(length=10):
return ''.join(
random.choices(string.ascii_uppercase + string.digits, k=length))
if __name__ == "__main__":
print("current ip:", current_ip())