python3 network speed test site

First, the operating environment

1、Windows 10

2、python 3.8

Second, the installation of third-party libraries pycurl

1, first install

pip install wheel

2, the mounting pycurl

https://download.lfd.uci.edu/pythonlibs/t7epjj8p/pycurl-7.43.0.3-cp38-cp38-win_amd64.whl

Third, test scripts

. 1  Import the pycurl
 2  Import OS, SYS
 . 3  Import Time
 . 4  Import SYS
 . 5  
. 6  
. 7  the try :
 . 8      the URL the sys.argv = [. 1]     # test site's domain name 
. 9  the except Exception AS E:
 10      Print ( " Error: " + STR (E ))
 11      Print ( " usage: Please enter the web address to probe " )
 12      sys.exit ()
 13  # the URL of = "http://www.baidu.com" domain # test site 
14 c = pycurl.Curl()    
15 c.setopt(pycurl.URL, URL)    
16 c.setopt(pycurl.CONNECTTIMEOUT, 5)    
17 c.setopt(pycurl.TIMEOUT, 5)    
18 c.setopt(pycurl.NOPROGRESS, 1)    
19 c.setopt(pycurl.FORBID_REUSE, 1)    
20 c.setopt(pycurl.MAXREDIRS, 1)    
21 c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)    
22 
23 indexfile = open(os.path.dirname(os.path.realpath(__file__))+"/content.txt", "wb")
24 c.setopt(pycurl.WRITEHEADER, indexfile)    
25 c.setopt(pycurl.WRITEDATA, indexfile)   
26 try:
27     c.perform()    #提交请求
28 except Exception as e:
29     print("connecion error:"+str(e))
30     indexfile.close()
31     c.close()
32     sys.exit()
33 
34 NAMELOOKUP_TIME =  c.getinfo(c.NAMELOOKUP_TIME)   
35 CONNECT_TIME =  c.getinfo(c.CONNECT_TIME)    
36 PRETRANSFER_TIME =   c.getinfo(c.PRETRANSFER_TIME)   
37 STARTTRANSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME)    
38                                                         
39 TOTAL_TIME = c.getinfo(c.TOTAL_TIME)    
40 HTTP_CODE =  c.getinfo(c.HTTP_CODE)    
41 SIZE_DOWNLOAD =  c.getinfo(c.SIZE_DOWNLOAD)   
42 HEADER_SIZE = c.getinfo(c.HEADER_SIZE)   
43 SPEED_DOWNLOAD=c.getinfo(c.SPEED_DOWNLOAD)    
44 
45 print("测试网站:",URL)
46 print("HTTP状态码:{}" .format(HTTP_CODE))
47  Print ( " the HTTP status code:% S " % (HTTP_CODE))
 48  Print ( " the DNS resolution time:.%. 2F MS " % (NAMELOOKUP_TIME * 1000 ))
 49  Print ( " establish a connection time:.%. 2F MS " % (CONNECT_TIME * 1000 ))
 50  Print ( " ready for transmission time:.%. 2F MS " % (PRETRANSFER_TIME * 1000 ))
 51 is  Print ( " transmission start time:.%. 2F MS " % (STARTTRANSFER_TIME * 1000 ))
 52 is  Print ( " end of transmission total time:.% 2F MS "% (* TOTAL_TIME 1000 ))
 53 is  Print ( " Download Packet Size:% bytes D / S " % (SIZE_DOWNLOAD))
 54 is  Print ( " the HTTP header size: D% byte " % (header_size))
 55  Print ( " Average download speed:% bytes D / S " % (SPEED_DOWNLOAD))
 56 is  # close files and objects Curl 
57 is  indexfile.close ()
 58 c.close ()

Fourth, verification

Guess you like

Origin www.cnblogs.com/ccip-ma/p/12033566.html