rafarataneneces Posted August 6, 2016 Posted August 6, 2016 Dear programmers I have a wonderful Python script that will allow me to turn on/off my FLUX/MagicLight bulbs. These bulbs are running HORRIBLY as for some reason they turn RED, then they change color, every single time I turn them on. Therefore, I learned how to turn them on/off via a python script that is over 1300+ lines long. I would like to somehow execute that server via a network service or I don't know a web server or a ssh in the ISY Any ideas?
larryllix Posted August 6, 2016 Posted August 6, 2016 Having a python interpreter on the ISY would be a huge breakthrough. All the hardware is there in ISY to support anything you want to do, and could not only eliminate all the second computer proxies/brides/nodes, but also be the lead-in for a multicore ISY995i. Unfortunately, I have never heard these capabilities.
MWareman Posted August 6, 2016 Posted August 6, 2016 You cannot run outside scrips on the ISY itself. It's operating system is not designed for general purpose use. Many on here use a device like a RaspberryPI, and have scripts by executable via a network call from the ISY.
rafarataneneces Posted August 7, 2016 Author Posted August 7, 2016 You cannot run outside scrips on the ISY itself. It's operating system is not designed for general purpose use. Many on here use a device like a RaspberryPI, and have scripts by executable via a network call from the ISY. Ok but if I save a python script on a remote computer, how can I execute that python script through a http call? I know how to set up Apache + PHP, I don't have experience with Python.
MWareman Posted August 7, 2016 Posted August 7, 2016 I do PHP as a cgi script under Apache. I've never done Python, but I'm sure you can execute the Python script from a small php script called in this way.
jtara92101 Posted August 7, 2016 Posted August 7, 2016 (edited) You don't need anything as complicated as setting-up an Apache server and PHP just to run a Python REST service! The standard Python library include a module that you can use to build simple services in just a few lines of code. For Python 2: https://docs.python.org/2/library/simplehttpserver.html Or Python 3: https://docs.python.org/3.4/library/http.server.html I haven't used this myself, but I have written simple services in a few lines of code in Ruby (using the Sinatra mini-framework) and Javascript (nodejs). Here are a couple of links showing simple examples. https://www.xplatform.rocks/2014/04/28/create-a-lightweight-rest-service-using-sinatra/ https://blog.nodejitsu.com/a-simple-webservice-in-nodejs/ The approach is very similar for all 3 languages. You use a powerful HTTP server class/module to create a server, and with only a little bit of code you can create a server that can easily examine requests and make replies. There is no need for a standalone web server. Edited August 7, 2016 by jtara92101
rafarataneneces Posted August 7, 2016 Author Posted August 7, 2016 You don't need anything as complicated as setting-up an Apache server and PHP just to run a Python REST service! The standard Python library include a module that you can use to build simple services in just a few lines of code. For Python 2: https://docs.python.org/2/library/simplehttpserver.html Or Python 3: https://docs.python.org/3.4/library/http.server.html I haven't used this myself, but I have written simple services in a few lines of code in Ruby (using the Sinatra mini-framework) and Javascript (nodejs). Here are a couple of links showing simple examples. https://www.xplatform.rocks/2014/04/28/create-a-lightweight-rest-service-using-sinatra/ https://blog.nodejitsu.com/a-simple-webservice-in-nodejs/ The approach is very similar for all 3 languages. You use a powerful HTTP server class/module to create a server, and with only a little bit of code you can create a server that can easily examine requests and make replies. There is no need for a standalone web server. Is there a Google App or something that I can create on the cloud?
jtara92101 Posted August 7, 2016 Posted August 7, 2016 Is there a Google App or something that I can create on the cloud? There are MANY, MANY "cloud"-based (whatever that really means...) services for running little REST servers, periodic cron jobs, etc. etc. Some are for a specific language/environment (for example, services that offer nothing else but running Sinatra servers, or nodejs servers, or...) some are more general. e.g. a low-end VPS, or Virtual Private Server. You probably don't want it on the cloud, though. Then you'd need to have your router redirect a port to the ISY, and open your ISY up to the Internet. Yes, you can use HTTPS. No, still not really a good idea... And then you are dependent on the Internet. Better to run it on a local device. An always-on computer. A Raspberry Pi. Your router, running open-source Linux-based router firmware.
rafarataneneces Posted August 7, 2016 Author Posted August 7, 2016 There are MANY, MANY "cloud"-based (whatever that really means...) services for running little REST servers, periodic cron jobs, etc. etc. Some are for a specific language/environment (for example, services that offer nothing else but running Sinatra servers, or nodejs servers, or...) some are more general. e.g. a low-end VPS, or Virtual Private Server. You probably don't want it on the cloud, though. Then you'd need to have your router redirect a port to the ISY, and open your ISY up to the Internet. Yes, you can use HTTPS. No, still not really a good idea... And then you are dependent on the Internet. Better to run it on a local device. An always-on computer. A Raspberry Pi. Your router, running open-source Linux-based router firmware. So if I install DD WRT on my router I can SSH to it and run Apache + PHP + Python? That would be awesome!! Has anybody done that?
larryllix Posted August 8, 2016 Posted August 8, 2016 (edited) You don't need anything as complicated as setting-up an Apache server and PHP just to run a Python REST service! The standard Python library include a module that you can use to build simple services in just a few lines of code. For Python 2: https://docs.python.org/2/library/simplehttpserver.html Or Python 3: https://docs.python.org/3.4/library/http.server.html I haven't used this myself, but I have written simple services in a few lines of code in Ruby (using the Sinatra mini-framework) and Javascript (nodejs). Here are a couple of links showing simple examples. https://www.xplatform.rocks/2014/04/28/create-a-lightweight-rest-service-using-sinatra/ https://blog.nodejitsu.com/a-simple-webservice-in-nodejs/ The approach is very similar for all 3 languages. You use a powerful HTTP server class/module to create a server, and with only a little bit of code you can create a server that can easily examine requests and make replies. There is no need for a standalone web server. For python3 where does this HTTPServer come from? I only ever get no such module. I have tried every import xxx I can think of and find in the referenced links. Even the docs have syntax errors in them so I suspect some are untested. UPDATE: I got it from another site from http.server import BaseHTTPRequestHandler, HTTPServer Thanks! Edited August 8, 2016 by larryllix
mwester Posted August 8, 2016 Posted August 8, 2016 So if I install DD WRT on my router I can SSH to it and run Apache + PHP + Python? That would be awesome!! Has anybody done that? In theory, you could do that. In the real world, there isn't enough storage or memory in a router to support an application written in python. If you are capable of installing DD-WRT and setting up Apache/PHP, etc, then you can also set up a Raspberry PI -- just do that, it'll be far, far easier.
rafarataneneces Posted August 8, 2016 Author Posted August 8, 2016 In theory, you could do that. In the real world, there isn't enough storage or memory in a router to support an application written in python. If you are capable of installing DD-WRT and setting up Apache/PHP, etc, then you can also set up a Raspberry PI -- just do that, it'll be far, far easier. I have the Netgear R7000 but I have doubts I will be able to run Apache + PHP + Python as I read DDWRT only allows Apache + PHP but no CGI (and I need that to execute Python) I am going to use a Raspberry PI at some moment, right now I am using Google App Engine and it works wonderful I don't want to buy a computer unless I see myself starting to write so many APIs that I am afraid the cloud will fail or I want to gain speed from a local IP address talking directly to my bulbs/ISY.
rafarataneneces Posted August 8, 2016 Author Posted August 8, 2016 Ladies and Gentlemen Thank you for all your suggestions!!I have a WD My Cloud EX4 It had python already installed! And it had an Apache server already installed and it executed cgi-bin files in a special folder already! All I had to do was create links from the /Shares/cgi-bin to /var/www/cgi-bin/ That's It!!!Now I can upload Python scripts (in this case, I am controlling my MagicLights) And I can turn them ON/OFF!!! All with Network Module from ISY994!!!
jtara92101 Posted August 8, 2016 Posted August 8, 2016 (edited) You do NOT need Apache! You do NOT need CGI! You do NOT need PHP! You need Python. Period. You can create one or more HTTP servers using the HTTP server package. It's only a few lines of code. Your Python program is the server. You might need to get the http module online using a package manager. I'm not a regular Python user, sorry. Your router with ddwrt is perfectly suited for running Python scripts. Unsure about ddwrt, but much of the OpenWrt UI is WRITTEN in Python. Edited August 8, 2016 by jtara92101
rafarataneneces Posted August 8, 2016 Author Posted August 8, 2016 you don't understand I found an Apache server in my house (WD EX4) perfectly capable of running Python scripts so I chose that route instead of buying a small computer just for that
jtara92101 Posted August 8, 2016 Posted August 8, 2016 you don't understand I found an Apache server in my house (WD EX4) perfectly capable of running Python scripts so I chose that route instead of buying a small computer just for that I do understand. Just pointing-out for the benefit of others that the Apache server, PHP, and CGI are all unnecessary. Might save somebody else a LOT of trouble! With the http server module, you can write a tiny dedicated server in just a few lines of code. You would typically use some port OTHER than port 80. (So it would not interfere with any existing web server running on the same machine.) You just write a small Python script and run it. It listens for HTTP connections on a port. Was not suggesting you buy a small computer. You found your always-on small computer! A stand-alone server just to run your REST service would divorce it from the complexities of Apache.
larryllix Posted August 8, 2016 Posted August 8, 2016 You do NOT need Apache! You do NOT need CGI! You do NOT need PHP! You need Python. Period. You can create one or more HTTP servers using the HTTP server package. It's only a few lines of code. Your Python program is the server. You might need to get the http module online using a package manager. I'm not a regular Python user, sorry. Your router with ddwrt is perfectly suited for running Python scripts. Unsure about ddwrt, but much of the OpenWrt UI is WRITTEN in Python. I've ben attempting use some of this python easy code and "a few lines of code" will probably turn out to be a few hundred before anything can be done, with any functionality. Python offers some BaseHTTPRequestHandler package along with a few others that support REST style commands. The package allows you to send back select information from a hit on your IP address and port. It doesn't matter what the URL contains the package is labelled as a "GET" and no decoding or simplicity is offered othe than handing the whole URL to you in a string variable. So now a complete parser is required to be written, as well as the downstream device handlers etc. The very basics may be there, but a long long way from just a few lines of code. It does get the URL call into python though.
jtara92101 Posted August 8, 2016 Posted August 8, 2016 I'd suggest doing some searches on creating REST servers in Python. I think you may want to look at using a "micro framework". It looks like Flask is popular: http://flask.pocoo.org/ Like I said, I don't write much Python. I would do this in Ruby myself, but only because I am more familiar with Ruby. Python is certainly more universal. For Ruby, I would use the Sinatra micro-framework. Flask looks similar.
larryllix Posted August 8, 2016 Posted August 8, 2016 I'd suggest doing some searches on creating REST servers in Python. I think you may want to look at using a "micro framework". It looks like Flask is popular: http://flask.pocoo.org/ Like I said, I don't write much Python. I would do this in Ruby myself, but only because I am more familiar with Ruby. Python is certainly more universal. For Ruby, I would use the Sinatra micro-framework. Flask looks similar. Thanks. For a very small and simple REST command centre it shouldn't be too bad, now that I am getting a handle on it somewhat. Maybe a hundred lines of python code should hook me into other modules to drive devices. Of course the REST interface module will have to expand every time I hook another into it to split the URLs into various jobs. Thanks for pointing me there in the first place. It gives me more hope for python hooks into the world. This is where I am lacking. Python is new and quite confusing as it is such a mess as is all the linux versions and the RPi in general. I never realised how bad lack standards can make our S.Jobs so tough.
rafarataneneces Posted August 8, 2016 Author Posted August 8, 2016 I already chose Google App Engine as my server, and my python script works great!!!I can turn on/off MagicLights (search for MagicLights on Amazon, AMAZING lights!!!) through internal API local network What I don't understand is if I have said that I had a Python code that I wanted to use, why this topic deviated to 'Which frameworks should I use to develop in Python??' I NEVER asked that question, it's ONE SIMPLE Python script and I wanted to know how to hook it up to Apache. At the end of the day, Google App Engine allowed me to deploy the Python script online, and then I hooked it up to WD My Cloud EX4 which already had a Apache Server. I appreciate the people that recommended Raspberry PI since that would be the cheapest way to hook it up if I wouldn't have found a solution.
larryllix Posted August 8, 2016 Posted August 8, 2016 (edited) <snippage> What I don't understand is if I have said that I had a Python code that I wanted to use, why this topic deviated to 'Which frameworks should I use to develop in Python??' I NEVER asked that question, it's ONE SIMPLE Python script and I wanted to know how to hook it up to Apache. At the end of the day, Google App Engine allowed me to deploy the Python script online, and then I hooked it up to WD My Cloud EX4 which already had a Apache Server. I appreciate the people that recommended Raspberry PI since that would be the cheapest way to hook it up if I wouldn't have found a solution. In your OP you asked"I would like to somehow execute that server via a network service or I don't know a web server or a ssh in the ISY" Read the thread again. 95% of this thread has been about connecting a RPi python program to an ISY, just as you requested. People were trying to help you, with some sidetracks, of course. Edited August 8, 2016 by larryllix
rafarataneneces Posted August 8, 2016 Author Posted August 8, 2016 But you can see I already said I chose Apache and aparently my solution is not welcome http://forum.universal-devices.com/topic/19536-how-do-i-execute-a-python-script-on-the-isy994/?do=findComment&comment=183818 So I don't know why blast if it's a solution that works then it works and I'm happy Apparently Apache + Python is a crime
Recommended Posts