我在pimylifeup.com的python代码“ speedtest.py”中添加了一行。我希望它能使我跟踪互联网提供商和IP地址以及他的代码提供的所有其他速度信息。但是,当我执行它时,代码只会在find all调用之后捕获下一个单词。我还希望它返回出现在提供程序之后的IP地址。我已经附上了下面的代码。您能帮我修改它以返回我想要的内容吗。
这里是一个例子,speedtest-cli返回的内容>
$ speedtest-cli Retrieving speedtest.net configuration... Testing from Biglobe (111.111.111.111)... Retrieving speedtest.net server list... Selecting best server based on ping... Hosted by GLBB Japan (Naha) [51.24 km]: 118.566 ms Testing download speed................................................................................ Download: 4.00 Mbit/s Testing upload speed...................................................................................................... Upload: 13.19 Mbit/s $
并且这是speediest.py返回我的.csv文件的示例
Date,Time,Ping,Download (Mbit/s),Upload(Mbit/s),myip 05/30/20,12:47,76.391,12.28,19.43,Biglobe
这是我要它返回的内容。
Date,Time,Ping,Download (Mbit/s),Upload (Mbit/s),myip 05/30/20,12:31,75.158,14.29,19.54,Biglobe 111.111.111.111
或者可能是,
05/30/20,12:31,75.158,14.29,19.54,Biglobe,111.111.111.111
这里是我正在使用的代码。并感谢您提供的任何帮助。
import os
import re
import subprocess
import time
response = subprocess.Popen(‘/usr/local/bin/speedtest-cli’, shell=True, stdout=subprocess.PIPE).stdout.read().decode(‘utf-8’)
ping = re.findall(‘km]:\s(.*?)\s’, response, re.MULTILINE)
download = re.findall(‘Download:\s(.*?)\s’, response, re.MULTILINE)
upload = re.findall(‘Upload:\s(.*?)\s’, response, re.MULTILINE)
myip = re.findall(‘from\s(.*?)\s’, response, re.MULTILINE)
ping = ping[0].replace(‘,’, ‘.’)
download = download[0].replace(‘,’, ‘.’)
upload = upload[0].replace(‘,’, ‘.’)
myip = myip[0]
try:
f = open(‘/home/pi/speedtest/speedtestz.csv’, ‘a+’)
if os.stat(‘/home/pi/speedtest/speedtestz.csv’).st_size == 0:
f.write(‘Date,Time,Ping,Download (Mbit/s),Upload (Mbit/s),myip\r\n’)
except:
pass
f.write(‘{},{},{},{},{},{}\r\n’.format(time.strftime(‘%m/%d/%y’), time.strftime(‘%H:%M’), ping, download, upload, myip))
我在pimylifeup.com的python代码“ speedtest.py”中添加了一行。我希望它可以让我跟踪互联网提供商和IP地址以及他的所有其他速度信息...
$python3 speedtest.py --csv-header > output.csv
$python3 speedtest.py --csv >> output.csv