FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-thingserver/thingserver/utils.py at master · labthings/python-thingserver · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Jun 7, 2021. It is now read-only.
labthings
/
python-thingserver
Public archive
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-thingserver
/
thingserver
/
utils.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (44 loc) · 1.34 KB
Breadcrumbs
python-thingserver
/
thingserver
/
utils.py
Copy path
File metadata and controls
60 lines (44 loc) · 1.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Utility functions."""
import
datetime
import
socket
import
ifaddr
def
timestamp
():
"""
Get the current time.
Returns the current time in the form YYYY-mm-ddTHH:MM:SS+00:00
"""
return
datetime
.
datetime
.
utcnow
().
strftime
(
"%Y-%m-%dT%H:%M:%S+00:00"
)
def
get_ip
():
"""
Get the default local IP address.
From: https://stackoverflow.com/a/28950776
"""
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
try
:
s
.
connect
((
"10.255.255.255"
,
1
))
ip
=
s
.
getsockname
()[
0
]
except
(
socket
.
error
,
IndexError
):
ip
=
"127.0.0.1"
finally
:
s
.
close
()
return
ip
def
get_addresses
():
"""
Get all IP addresses.
Returns list of addresses.
"""
addresses
=
set
()
for
iface
in
ifaddr
.
get_adapters
():
for
addr
in
iface
.
ips
:
# Filter out link-local addresses.
if
addr
.
is_IPv4
:
ip
=
addr
.
ip
if
not
ip
.
startswith
(
"169.254."
):
addresses
.
add
(
ip
)
elif
addr
.
is_IPv6
:
# Sometimes, IPv6 addresses will have the interface name
# appended, e.g. %eth0. Handle that.
ip
=
addr
.
ip
[
0
].
split
(
"%"
)[
0
].
lower
()
if
not
ip
.
startswith
(
"fe80:"
):
addresses
.
add
(
"[{}]"
.
format
(
ip
))
return
sorted
(
list
(
addresses
))
Back
|
FazBrowse Home
|
New Git URL