feverish
Posts: 497
Joined: Wed Jun 27, 2012 2:29 pm

Cherrypy.config.update problem

Fri Sep 26, 2014 4:00 pm

With huge guidance from here, I have at last a Python + Cherrypy app which sometimes works, but only locally on the Raspi. During the (locally-only) signon,

Code: Select all

 python3  appname.py
lxterminal announces there is no config file. In that case, it also reports starting on 127.0.0.1:8080 and when I use Iceweasel or Midori on the Raspi I am happy with the result. However, I need to view the app's display remotely over our LAN. I know the Raspi's IP address, so I added

Code: Select all

cherrypy.config.update({'server.socket.host' : '0.0.0.0', 'server.socket.port' : 8085,})

to allow me to do this, but no matter where I try inserting the cherrypy.update thing, or indenting it, lxterminal either reports a syntax fault, or goes on to close it down again, when the problem seems to stem from the on/off toggle def.(says he, not knowing much)
The module's code is:-

Code: Select all

import cherrypy
from ds18b20 import DS18B20
class HeaterService(object):
        def __init__(self):
            self.dal = DS18B20()
            self.heaterState = "OFF"
        @cherrypy.expose
        def index(self):
            temperatures = self.dal.get_temperatures([DS18B20.DEGREES_F])
            btnText = "off" if self.heaterState == "ON" else "on"
           return '''<html>
    <meta http-equiv="refresh" content="6;url="http://index">
    <script type="text/javascript">var currentTemp = {0:f};</script>
    <body>
    <div>Current temperature is:</div>
    <div>{0:.2f}</div>
    <div>degrees Fahrenheit</div>
    <div>Heater state is: {1}</div>
    <div><a href="toggle">Turn heater {2}</a>
    </body></html>'''.format(temperatures[0], self.heaterState, btnText)
        @cherrypy.expose
        def toggle(self):
            if self.heaterState == "OFF":
                # turn the heater on here
                self.heaterState = "ON"
            else:
                # turn the heater off here
                self.heaterState = "OFF"
            raise cherrypy.HTTPRedirect("/index")

if __name__ == '__main__':
       cherrypy.quickstart(HeaterService())
cherrypy.config.update({ 'server.socket.host': '0.0.0.0','server.socket.port':8085,}) #this does not worki as required
At the risk of offending anyone who may think I've been taught enough already, could anyone who knows please spare some time to teach me more?

UPDATE:
I have also tried using

Code: Select all

index.exposed = True
index._cp_config = {'server.socket_host' : '0.0.0.0', 'server.socket_port' : 8085,} 
immediately following the <body> line but that has no apparent effect at all

Now 'WORKING', if that is what 'working' meansBUT what a lousy Kludge>>>>>>
The problem was in the <meta> tag in the code. I had to comment this out, then start the app, which did change the host and the port, then edit the .py file to remove the # commenter while the app was still running, and save it. Surely there must be a 'proper' way to do this?
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

User avatar
r3d4
Posts: 993
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Cherrypy.config.update problem

Fri Oct 03, 2014 9:15 am

I dont use this / i know knothing of it!

but ..

Code: Select all

 8085,})
the trailing comma (in bold ) looks out of place !
perhaps try removing it .

like so

Code: Select all

 8085})
:?:
* ....(generally ) a comma is used to seperate "Key":"value" pairs when they are contained in an object {} or "values" when contained in an array [] .

so (i think ) the syntax error is that the "key" : "value" pair--> 'server.socket_port' : 8085
is the last one contained within the ` index._cp_config ` object so no comma is needed . ( hope that is understandable :mrgreen: )

*im thinking of json / javascript but i think it not exclusive to this . ,.. might be wrong !
Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?

billio
Posts: 71
Joined: Thu Dec 15, 2011 8:25 am
Contact: Website

Re: Cherrypy.config.update problem

Fri Oct 03, 2014 8:43 pm

Just looking at your code, the return line in def index(self): is incorrectly indented.
Also, in the html, the meta tag should be within <head></head> tags.
And in your meta tag, the content attribute is not properly quoted: "6"
In the examples I have seen, cherrypy.config.update() is used before cherrypy.quickstart() and properly indented.
But I am not an expert in cherrypy.
Hope this helps.

User avatar
Douglas6
Posts: 4995
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Cherrypy.config.update problem

Sat Oct 04, 2014 12:53 am

billio wrote:Just looking at your code, the return line in def index(self): is incorrectly indented.
Also, in the html, the meta tag should be within <head></head> tags.
And in your meta tag, the content attribute is not properly quoted: "6"
In the examples I have seen, cherrypy.config.update() is used before cherrypy.quickstart() and properly indented.
But I am not an expert in cherrypy.
Hope this helps.
Just ran this code. The indentation of def index(self) IS correct, but a couple lines below, return '''<html> needs one more space. [EDIT: Oh yes, that's what billio said]
Mozilla (bless it's heart) doesn't care about the <meta/> tags, or even the mismatched "6, but those should be corrected. Other browsers (IE) won't be as forgiving.
The cherrypy.config.update() definitely needs to proceed the cherrypy.quickstart. My working code looks like this:

Code: Select all

if __name__ == '__main__':    
    cherrypy.config.update({'server.socket_host': '0.0.0.0'})
    cherrypy.quickstart(HeaterService())
With that, I'm able to access the page on another PC on the local network via

Code: Select all

http://192.168.1.71:8080/index
If you're running Avahi (Bonjour), you can replace the Pi's IP address (use your own, of course) with the Pi's hostname.

I originally wrote (and tested) that code for Python 2.7, can't guarantee it works in Python3.

Finally, it helps to be really 'anal' about your indenting in Python. Understand what it means and why it's important. Train yourself to use four spaces (never tabs) for each level of indentation. This will work for both Python 2 and 3 (though of course there are other differences).

Return to “Python”