calling a python function from flask app -


i developing app runs (always - controller heat pump system) in python , use flask provide user interface controll app.

the flask app has different control items, instance buttons turn system on or off.

i trying execute specific function python module in response "click" on button (the final goal change value in mmap resource read in module change state of system).

in flask app have like:

    @app.route('/cntr_hpauto',methods=['get','post'])     @basic_auth.required     def cntr_hpauto():         manage_globals.set_from_web()         return render_template('control.html',cur_hp_mode="auto") 

however, generates "internal server error'

the complete flask app (manage_globals *.py file want import , contains function want call):

from flask import flask, request, render_template flask_basicauth import basicauth  import sys import os import mmap import manage_globals  app = flask(__name__)  app.config['basic_auth_username'] = '***' app.config['basic_auth_password'] = '***'  basic_auth = basicauth(app)  @app.route('/')  def splash():         return render_template('splash.html')  @app.route('/dashboard', methods=['get','post']) @basic_auth.required def dashboard():         return render_template('dashboard.html')  @app.route('/control',methods=['get','post']) @basic_auth.required def control():     return render_template('control.html',cur_hp_mode="none")  @app.route('/cntr_hpauto',methods=['get','post']) @basic_auth.required def cntr_hpauto():     manage_globals.set_from_web()     return render_template('control.html',cur_hp_mode="auto")  @app.route('/cntr_hpon',methods=['get','post']) @basic_auth.required def cntr_hpon():     return render_template('control.html',cur_hp_mode="on")  @app.route('/cntr_hpoff',methods=['get','post']) @basic_auth.required def cntr_hpoff():     return render_template('control.html',cur_hp_mode="off")  if __name__ == '__main__':     app.run(ssl_context=('/home/groenhol/certs/groenhol.pem', '/home/groenhol/certs/groenhol.key')) 

and module (example, writing map file logfile) is:

# 14/08/2017 henk witte / groenholland # part of geotech project, ann controller dual source heat pump # module maintains global database mmap  import mmap  """ mmap file position dependent! use readlines , split      line 1: heatpump auto/on/off     line 2: userpump off     line 3: srcselect air """ def init_conf_file():     dummy="a"  def set_from_web():     open("geotech.conf", "r+b") f:         mm = mmap.mmap(f.fileno(), 0)         line in iter(mm.readline, b''):             open("globals.log","ab") f2:                 f2.write(line)     f2.close()     mm.close()  if __name__ == '__main__':     init_conf_file() 

the flask app runs fine without function call, module import runs fine well.

any appreciated!

henk

as suggested kevin pasquarella added app.debug = true. however, error occurs when apache loadin main splash page (apache server error) did not help. looked @ apache error log:

[tue aug 15 21:33:14.638580 2017] [mpm_event:notice] [pid 959:tid 3067240448] ah00489: apache/2.4.18 (ubuntu) openssl/1.0.2g mod_wsgi/4.5.17 python/3.4 configured -- resuming normal operations [tue aug 15 21:33:14.639152 2017] [core:notice] [pid 959:tid 3067240448] ah00094: command line: '/usr/sbin/apache2' [tue aug 15 21:33:19.825211 2017] [wsgi:error] [pid 2461:tid 3031819312] [remote 192.168.178.85:9676] mod_wsgi (pid=2461): target wsgi script '/home/groenhol/py_control/ui/webapp/main.wsgi' cannot loaded python module. [tue aug 15 21:33:19.826502 2017] [wsgi:error] [pid 2461:tid 3031819312] [remote 192.168.178.85:9676] mod_wsgi (pid=2461): exception occurred processing wsgi script '/home/groenhol/py_control/ui/webapp/main.wsgi'. [tue aug 15 21:33:19.967421 2017] [wsgi:error] [pid 2461:tid 3031819312] [remote 192.168.178.85:9676] traceback (most recent call last): [tue aug 15 21:33:19.970377 2017] [wsgi:error] [pid 2461:tid 3031819312] [remote 192.168.178.85:9676]   file "/home/groenhol/py_control/ui/webapp/main.wsgi", line 4, in <module> [tue aug 15 21:33:19.970581 2017] [wsgi:error] [pid 2461:tid 3031819312] [remote 192.168.178.85:9676]     main import app application [tue aug 15 21:33:19.971031 2017] [wsgi:error] [pid 2461:tid 3031819312] [remote 192.168.178.85:9676]   file "/home/groenhol/py_control/ui/webapp/main.py", line 41 

i searched mod_wsgi cannot loaded python module

answers indicate there difference between python version using (3.4) , wsgi version.

so checked wsgi version in /etc/apache2/mods-enabled/mod-wsgi.load:

loadmodule wsgi_module "/home/groenhol/miniconda3/lib/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so" wsgipythonhome "/home/groenhol/miniconda3"

so seems use python 3.4 version.

to make sure use ldd found during search:

groenhol@arm:~/mod_wsgi-4.5.15$ ldd loadmodule wsgi_module "/home/groenhol/miniconda3/lib/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so" loadmodule: ldd: ./loadmodule: no such file or directory wsgi_module: ldd: ./wsgi_module: no such file or directory /home/groenhol/miniconda3/lib/python3.4/site-packages/mod_wsgi/server/mod_wsgi-py34.cpython-34m.so:         linux-vdso.so.1 =>  (0xbee90000)         libpython3.4m.so.1.0 => /home/groenhol/miniconda3/lib/libpython3.4m.so.1.0 (0xb6d40000)         libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0xb6d0f000)         libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6c23000)         /lib/ld-linux-armhf.so.3 (0x7f64d000)         libdl.so.2 => /lib/arm-linux-gnueabihf/libdl.so.2 (0xb6c10000)         libutil.so.1 => /lib/arm-linux-gnueabihf/libutil.so.1 (0xb6bfd000)         libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0xb6b85000)         libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0xb6b5c000) groenhol@arm:~/mod_wsgi-4.5.15$ wsgipythonhome "/home/groenhol/miniconda3" -bash: wsgipythonhome: command not found 

as far can tell (http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#python-shared-library) seems ok?

ok, next step?

the code:

def set_from_web():     open("geotech.conf", "r+b") f:         mm = mmap.mmap(f.fileno(), 0)         line in iter(mm.readline, b''):             open("globals.log","ab") f2:                 f2.write(line)     f2.close()     mm.close() 

is going problem because using relative path name files.

the current working directory of process not code , not writable apache user. need use absolute paths , ensure apache user has write permission files.

see:


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -