首頁 > 後端開發 > Python教學 > 怎麼利用Python實現網路測試

怎麼利用Python實現網路測試

王林
發布: 2023-05-17 18:41:05
轉載
2118 人瀏覽過

Speedtest CLI 專為軟體開發人員、系統管理員和電腦愛好者等打造,是 Ookla® 提供技術支援的首款正式 Linux 本機 Speedtest 應用程式。

Python語言編寫的Speedtest CLI可在命令列直接運行,實現網路速度測試。也可以直接作為python模組在python IDE中呼叫。

首先,先看看如何在python應用程式中進行調用,使用pip直接安裝。

1

pip install speedtest-cli

登入後複製

將該模組直接匯入到我們目前的程式碼區塊中。

1

import speedtest as spt

登入後複製

建立網路測試物件

1

spd = spt.Speedtest()

登入後複製

列印目前可用於測試的伺服器清單

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

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

from pprint import pprint

 

pprint(spd.get_servers())

 

# {721.5702755019188: [{'cc': 'CN',

#                       'country': 'China',

#                       'd': 721.5702755019188,

#                       'host': 'speedtest1.he.chinamobile.com:8080',

#                       'id': '41912',

#                       'lat': '38.0428',

#                       'lon': '114.5149',

#                       'name': '石家庄',

#                       'sponsor': 'China Mobile Hebei Co., Ltd',

#                       'url': 'http://speedtest1.he.chinamobile.com:8080/speedtest/upload.php'}],

#  776.2668436087947: [{'cc': 'CN',

#                       'country': 'China',

#                       'd': 776.2668436087947,

#                       'host': '5gtest.shangdu.com:8080',

#                       'id': '36646',

#                       'lat': '34.7466',

#                       'lon': '113.6253',

#                       'name': 'Zhengzhou',

#                       'sponsor': 'China Unicom HeNan 5G',

#                       'url': 'http://5gtest.shangdu.com:8080/speedtest/upload.php'}],

#  1051.7168853741107: [{'cc': 'MN',

#                        'country': 'Mongolia',

#                        'd': 1051.7168853741107,

#                        'host': 'speedtest.gemnet.mn:8080',

#                        'id': '2853',

#                        'lat': '47.9200',

#                        'lon': '106.9200',

#                        'name': 'Ulaanbaatar',

#                        'sponsor': 'Gemnet LLC',

#                        'url': 'http://speedtest.gemnet.mn:8080/speedtest/upload.php'},

#                       {'cc': 'MN',

#                        'country': 'Mongolia',

#                        'd': 1051.7168853741107,

#                        'host': 'speedtest1.kewiko.mn:8080',

#                        'id': '30772',

#                        'lat': '47.9200',

#                        'lon': '106.9200',

#                        'name': 'Ulaanbaatar',

#                        'sponsor': 'Kewiko LLC',

#                        'url': 'http://speedtest1.kewiko.mn:8080/speedtest/upload.php'}],

#  1339.1170164273938: [{'cc': 'CN',

#                        'country': 'China',

#                        'd': 1339.1170164273938,

#                        'host': '5gnanjing.speedtest.jsinfo.net:8080',

#                        'id': '26352',

#                        'lat': '32.0500',

#                        'lon': '118.7667',

#                        'name': 'Nanjing',

#                        'sponsor': 'China Telecom JiangSu 5G',

#                        'url': 'http://5gnanjing.speedtest.jsinfo.net:8080/speedtest/upload.php'}],

#  1340.7612716854985: [{'cc': 'CN',

#                        'country': 'China',

#                        'd': 1340.7612716854985,

#                        'host': 'speedtest02.js165.com:8080',

#                        'id': '13704',

#                        'lat': '32.0602',

#                        'lon': '118.7968',

#                        'name': 'Nanjing',

#                        'sponsor': 'China Unicom',

#                        'url': 'http://speedtest02.js165.com:8080/speedtest/upload.php'}],

#  1381.9129755930571: [{'cc': 'CN',

#                        'country': 'China',

#                        'd': 1381.9129755930571,

#                        'host': 'speedtest.zjmobile.com:8080',

#                        'id': '17320',

#                        'lat': '32.2069',

#                        'lon': '119.4490',

#                        'name': 'ZhenJiang',

#                        'sponsor': 'China Mobile JiangSu 5G',

#                        'url': 'http://speedtest.zjmobile.com:8080/speedtest/upload.php'}],

#  1489.08809618835: [{'cc': 'RU',

#                      'country': 'Russia',

#                      'd': 1489.08809618835,

#                      'host': 'speedtest-ude.edinos.ru:8080',

#                      'id': '36254',

#                      'lat': '51.8336',

#                      'lon': '107.5840',

#                      'name': 'Ulan-Ude',

#                      'sponsor': 'EDINOS',

#                      'url': 'http://speedtest-ude.edinos.ru:8080/speedtest/upload.php'}],

#  1542.170901504592: [{'cc': 'RU',

#                       'country': 'Russia',

#                       'd': 1542.170901504592,

#                       'host': 'speedtest.bteleport.ru:8080',

#                       'id': '18543',

#                       'lat': '52.2757',

#                       'lon': '104.3087',

#                       'name': 'Irkutsk',

#                       'sponsor': 'Baikal Teleport',

#                       'url': 'http://speedtest.bteleport.ru:8080/speedtest/upload.php'},

#                      {'cc': 'RU',

#                       'country': 'Russia',

#                       'd': 1542.170901504592,

#                       'host': 'speedtest-irkutsk.fttb.beeline.ru:8080',

#                       'id': '31472',

#                       'lat': '52.2757',

#                       'lon': '104.3087',

#                       'name': 'Irkutsk',

#                       'sponsor': 'Beeline',

#                       'url': 'http://speedtest-irkutsk.fttb.beeline.ru:8080/speedtest/upload.php'}]}

登入後複製

取得目前最佳的測試伺服器

1

2

spd.get_best_server()

print('测试开始,请稍等...')

登入後複製

取得目前的下載速度

1

download = int(spd.download() / 1024 / 1024)

登入後複製

取得目前的上傳速度

1

2

3

4

5

upload = int(spd.upload() / 1024 / 1024)

 

print(f'当前下载速度为:{str(download)} MB/s')

print(f'当前上传速度为:{str(upload)} MB/s')

print('测试已完成!')

登入後複製

列印出最終的返回結果

測試開始,請稍等...
目前下載速度為:12 MB/s
目前上傳速度為:13 MB/s
測試已完成!

以上是怎麼利用Python實現網路測試的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板