Created
June 20, 2020 09:04
-
-
Save nickfox-taterli/6d7c48b69e54b0d03980f17e0437aa2c to your computer and use it in GitHub Desktop.
从Cloudflare下载速度测试
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import csv | |
import time | |
import requests | |
from urllib3.exceptions import InsecureRequestWarning | |
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) | |
with open('cf_ips.txt') as fo: | |
with open('speedtest_result.csv','w')as fc: | |
f_csv = csv.writer(fc) | |
f_csv.writerow(['IP','Cache','Length','Elapsed','Speed(MB/s)']) | |
for line in fo: | |
ip = line.replace('\n','') | |
with io.BytesIO() as f: | |
try: | |
start = time.time() | |
r = requests.get('http://%s/files/10MB.bin' % ip, timeout = 10, headers = {'Host':'www.accessoutside.tech'},stream = True,verify = False) | |
total_length = r.headers.get('Content-Length') | |
if total_length is None: # no content length header | |
f.write(r.content) | |
else: | |
for chunk in r.iter_content(1024): | |
f.write(chunk) | |
end = time.time() | |
print (ip + '[' + r.headers['CF-Cache-Status'] + ']' + ' 速度 ' + str(round(( int(total_length) / 1024 / 1024) / (end - start),2)) + ' MB/s') | |
f_csv.writerow([ip,r.headers['CF-Cache-Status'],total_length,round(end - start,2),round(( int(total_length) / 1024 / 1024) / (end - start),2)]) | |
except KeyboardInterrupt: | |
exit(0) | |
except : | |
print(ip + ' 超时!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment