Python3, 6 lines of code, get the network speed measuring artifact, I directly uninstall a certain 60 speed measuring device.

1 Introduction

Little Diaosi : Brother Yu, do you know the library speedtest?
Xiaoyu : Well, I know a little bit, what's the matter?
Little Diaosi : Then you teach me how to use it?
Xiaoyu : Do I need to teach you this? AIGC can answer your questions.
Little Diaosi : Oh, you are better than AIGC.
Xiaoyu : ... I am here with you, it is just a tool.
Xiao Diaosi : No, no, I mean, your answer is more in line with my idea than AIGC's answer.
Xiaoyu : I... well, then you can say it again.
Little Diaosi : I say your answer is perfect.
insert image description here

Xiaoyu : Hehe...I said it, I said it all!

2. Code combat

2.1 Introduction

2.1.1 Definition

  • speedtest is a Python library for testing network speed.
  • It uses the API of the Speedtest.net website to test the download and upload speed of the network connection, and returns the test results.
  • The speedtest library can be used to easily test the speed of network connections, and can be used to monitor network performance and diagnose network problems.

2.1.2 Common methods

  • There are 5 commonly used methods, as follows:
    • speedtest.Speedtest(): Create a Speedtest object.
    • get_best_server(): Find and return the best server.
    • download(): Performs a download speed test and returns the download speed in bits per second.
    • upload(): Performs an upload speed test and returns the upload speed in bits per second.
    • results.dict(): Returns all test results in dictionary form, including Ping delay, download speed, upload speed, etc.

2.1.3 Function

  • Test the download and upload speed of the network to help developers evaluate network performance.
  • Provides the choice of the best server to ensure accurate test results.
  • Return detailed test results, including Ping delay, download speed, upload speed and other information, which is convenient for developers to analyze and record.

2.2 Installation

Because gtts is a third-party library, it needs to be installed before use:

pip install speedtest-cli

For other installation methods, you can refer to these two articles:

After the installation is complete, you can write code.

2.3 Examples

2.3.1 Test upload and download speed

code example

# -*- coding:utf-8 -*-
# @Time   : 2023-07-07
# @Author : Carl_DJ
'''
实现功能:
	测试上传与下载速度
'''
import speedtest

result = speedtest.net()

upload_speed = result.upload_speed
download_speed = result.download_speed

2.3.2 Test delay

code example

# -*- coding:utf-8 -*-
# @Time   : 2023-07-07
# @Author : Carl_DJ
'''
实现功能:
	测试延迟,  如:ping
'''
import speedtest

result = speedtest.net()

ping_speed = result.ping_speed

2.3.3 Custom server test

code example

# -*- coding:utf-8 -*-
# @Time   : 2023-07-07
# @Author : Carl_DJ
'''
实现功能:
	测试服务器速度
'''
import speedtest

#测试CSDN服务器,嘿嘿..
result = speedtest.net(server='https://blog.csdn.net/')

upload_speed = result.upload_speed
download_speed = result.download_speed

2.3.4 Multi-connection test

code example

# -*- coding:utf-8 -*-
# @Time   : 2023-07-07
# @Author : Carl_DJ
'''
实现功能:
	测试多个链接
'''
import speedtest

#测试 C站和百度
result1 = speedtest.net(server='https://blog.csdn.net/', test_number=1)
result2 = speedtest.net(server='www.baidu.com', test_number=2)

upload_speed1 = result1.upload_speed
download_speed1 = result1.download_speed

upload_speed2 = result2.upload_speed
download_speed2 = result2.download_speed

2.3.5 Actual Combat

# -*- coding:utf-8 -*-
# @Time   : 2023-07-07
# @Author : Carl_DJ
'''
实现功能:
	网络测速器
'''


import speedtest as st

# 设置最佳测试服务区
server = st.Speedtest()
server.get_best_server()

# 测试下载速度
down_sp = server.download()
down = down_sp  / 1000000
print(f"下载网速为: {
      
      down_sp } Mb/s")

# 测试上传网速
up_sp = server.upload()
up = up_sp  / 1000000
print(f"上传网速为: {
      
      up } Mb/s")

# 测试ping速度
ping = server.results.ping
print(f"Ping 速度: {
      
      ping}")

3. Summary

Seeing this, today's sharing is almost over. Today I mainly share the speed test of uploading, downloading and so on
by using the speedtest library.

I am a small fish :

  • CSDN blog expert ;
  • Aliyun expert blogger ;
  • 51CTO blog expert ;
  • 51 certified lecturer, etc .;
  • Certified gold interviewer ;
  • Job interview and training planner ;
  • Certified expert bloggers in several domestic mainstream technical communities ;
  • Winners of the first and second prizes in the evaluation of various mainstream products (Alibaba Cloud, etc.) ;

Follow me and take you to learn more, more professional and prefaced Python technology.

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/131597992