FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythoncode-tutorials/general/network-usage/network_usage.py at master · mgolas/pythoncode-tutorials · GitHub
mgolas
/
pythoncode-tutorials
Public
forked from
x4nth055/pythoncode-tutorials
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythoncode-tutorials
/
general
/
network-usage
/
network_usage.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
34 lines (29 loc) · 1.11 KB
Breadcrumbs
pythoncode-tutorials
/
general
/
network-usage
/
network_usage.py
Copy path
File metadata and controls
34 lines (29 loc) · 1.11 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
import
psutil
import
time
UPDATE_DELAY
=
1
# in seconds
def
get_size
(
bytes
):
"""
Returns size of bytes in a nice format
"""
for
unit
in
[
''
,
'K'
,
'M'
,
'G'
,
'T'
,
'P'
]:
if
bytes
<
1024
:
return
f"
{
bytes
:.2f
}
{
unit
}
B"
bytes
/=
1024
# get the network I/O stats from psutil
io
=
psutil
.
net_io_counters
()
# extract the total bytes sent and received
bytes_sent
,
bytes_recv
=
io
.
bytes_sent
,
io
.
bytes_recv
while
True
:
# sleep for `UPDATE_DELAY` seconds
time
.
sleep
(
UPDATE_DELAY
)
# get the stats again
io_2
=
psutil
.
net_io_counters
()
# new - old stats gets us the speed
us
,
ds
=
io_2
.
bytes_sent
-
bytes_sent
,
io_2
.
bytes_recv
-
bytes_recv
# print the total download/upload along with current speeds
print
(
f"Upload:
{
get_size
(
io_2
.
bytes_sent
)
}
"
f", Download:
{
get_size
(
io_2
.
bytes_recv
)
}
"
f", Upload Speed:
{
get_size
(
us
/
UPDATE_DELAY
)
}
/s "
f", Download Speed:
{
get_size
(
ds
/
UPDATE_DELAY
)
}
/s "
,
end
=
"
\r
"
)
# update the bytes_sent and bytes_recv for next iteration
bytes_sent
,
bytes_recv
=
io_2
.
bytes_sent
,
io_2
.
bytes_recv
Back
|
FazBrowse Home
|
New Git URL