Newbie python scripting question

Posted on
Wed Feb 01, 2017 9:15 am
aderrington offline
Posts: 116
Joined: Feb 03, 2015

Newbie python scripting question

I'm trying to create an Internet speed test script that will run and report the download speed to a variable, the variable would be watched by a charting plugin and would then graph the speed throughout the day (the script would run every hour)

My script so far is:

Code: Select all
import requests

url = 'http://ipv4.download.thinkbroadband.com/100MB.zip'
requests.get(url)


This downloads a 100Mb file. How can i run this and get the result? I can run it in terminal and it works fine, (i installed wget etc to get it to work)

Any thoughts?
Thanks in advance.
Andrew

EDIT -
Sorry, just realised that the command i was actually running in terminal is:
Code: Select all
wget -0 /dev/null http://ipv4.download.thinkbroadband.com/100MB.zip

But i cut it down to get it to run in Python, But i don't get any feedback once it's complete.
Last edited by aderrington on Wed Feb 01, 2017 9:18 am, edited 1 time in total.

Posted on
Wed Feb 01, 2017 9:17 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Newbie python scripting question

What do you mean by "get the result"? Do you mean save the file? Or record the time it takes to download the file?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Feb 01, 2017 9:20 am
aderrington offline
Posts: 116
Joined: Feb 03, 2015

Re: Newbie python scripting question

In terminal it gives a transfer speed
2017-02-01 14:50:55 (11.8 MB/s) - ‘/dev/null’ saved [104857600/104857600]


It's the transfer speed that i'm after.

Thanks.

Posted on
Wed Feb 01, 2017 9:30 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Newbie python scripting question

That's wget reporting the transfer speed, so using requests to transfer the file isn't going to do anything useful.

You could run wget in a subprocess and parse the result string to get that number.

Or, if it was me, I'd use speedtest-cli.

Code: Select all
sudo pip install speedtest-cli

Code: Select all
import speedtest

s = speedtest.Speedtest()
s.get_best_server()
s.download()

results = s.results.dict()
download  = results["download"]

See https://github.com/sivel/speedtest-cli for more info.

Code: Select all
Shamu:~ jkeenan$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import speedtest

>>> s = speedtest.Speedtest()

>>> s.get_best_server()
{'latency': 17.969, 'name': 'Bonita Springs, FL', 'url': 'http://stosat-bspr-01.sys.comcast.net/speedtest/upload.php', 'country': 'United States', 'lon': '-81.7387', 'cc': 'US', 'host': 'stosat-bspr-01.sys.comcast.net:8080', 'sponsor': 'Comcast', 'url2': 'http://a-stosat-bspr-01.sys.comcast.net/speedtest/upload.php', 'lat': '26.3548', 'id': '8087', 'd': 36.117175127976374}

>>> s.download()
85160228.89793384

>>> s.upload()
11419778.686360242

>>> print s.results.dict()
{'download': 85160228.89793384, 'timestamp': '2017-02-01T15:26:43.100689', 'ping': 17.969, 'upload': 11419778.686360242, 'server': {'latency': 17.969, 'name': 'Bonita Springs, FL', 'url': 'http://stosat-bspr-01.sys.comcast.net/speedtest/upload.php', 'country': 'United States', 'lon': '-81.7387', 'cc': 'US', 'host': 'stosat-bspr-01.sys.comcast.net:8080', 'sponsor': 'Comcast', 'url2': 'http://a-stosat-bspr-01.sys.comcast.net/speedtest/upload.php', 'lat': '26.3548', 'id': '8087', 'd': 36.117175127976374}}
>>>

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Feb 01, 2017 9:55 am
aderrington offline
Posts: 116
Joined: Feb 03, 2015

Re: Newbie python scripting question

Ok. Thanks for that. I'll give that a go later when i get home.

Thank you.


Sent from my iPhone using Tapatalk

Posted on
Thu Feb 02, 2017 4:42 pm
aderrington offline
Posts: 116
Joined: Feb 03, 2015

Re: Newbie python scripting question

Thanks for your suggestions, I've got it all working nicely now.

Thank you.
Andrew

Posted on
Thu Feb 02, 2017 4:52 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Newbie python scripting question

Glad to hear. That looks pretty useful, so I think I'll probably wrap it in a plugin.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Feb 02, 2017 4:53 pm
aderrington offline
Posts: 116
Joined: Feb 03, 2015

Re: Newbie python scripting question

That would be great. I've got it currently as a schedule running every 30 mins.

Thanks!


Sent from my iPhone using Tapatalk

Posted on
Thu Feb 02, 2017 5:26 pm
kmarkley offline
Posts: 185
Joined: Nov 15, 2016

Re: Newbie python scripting question

This sounds like a very logical addition to my Online Sensor plugin. I will add it this weekend.
Last edited by kmarkley on Sat Feb 04, 2017 1:33 pm, edited 1 time in total.

Posted on
Sat Feb 04, 2017 1:32 pm
kmarkley offline
Posts: 185
Joined: Nov 15, 2016

Re: Newbie python scripting question

Speedtest devices are now included in my Online Sensor plugin.

I hope you find it useful.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests