Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flask: Send form POST data to Python script #20

Closed
jeff1evesque opened this issue Aug 21, 2014 · 16 comments · Fixed by #32
Closed

Flask: Send form POST data to Python script #20

jeff1evesque opened this issue Aug 21, 2014 · 16 comments · Fixed by #32

Comments

@jeff1evesque
Copy link
Owner

We will research which python web framework to implement for receiving POST data from an HTML5 <form> element, which we hope to send to a python script.

@jeff1evesque
Copy link
Owner Author

Since we are using php as a primary means for our web pages, we do not need the full features django offers. We are using python as a programming language to handle the data, and analysis for support vector machine computations (and related, computational intensive tasks). Therefore, we will choose to use Flask as our Python Web Framework.

IRC #python (08/20/14 ~ 9:20pm EST):

jeffreylevesque: What is generally the preferred web framework in python?

papna: jeffreylevesque: Django is a very widely used framework.
papna: jeffreylevesque: Pyramid is a popular framework that has shaken off more cruft than Django.
papna: jeffreylevesque: Flask is a popular minimalist framework.

jeffreylevesque: Ah I was just going to ask about flask

papna: jeffreylevesque: There isn't "the preferred web framework".

varsis: having worked with Flask, it's pretty damn good if you don't need an entire toolbox like Django

papna: jeffreylevesque: Django is by far the most popular, but not as 'by far' as, say, Rails.

nedbat: jeffreylevesque: django vs flask: django gives you everything in the box, flask you put pieces together yourself. Django is better at choosing good defaults.

varsis: Flask for programmers, Django for web developers? 

jeff1evesque added a commit that referenced this issue Aug 22, 2014
jeff1evesque added a commit that referenced this issue Aug 22, 2014
@jeff1evesque
Copy link
Owner Author

b49b1d3: After installing flask, we changed route('/') to route('/tests/python/index.py') within index.py:

## index.py
#  This file currently tests the basic implementation of the 'Flask',
#  web-framework.

#!/usr/bin/python

from flask import Flask
app = Flask(__name__)

@app.route('/tests/python/index.py')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

Now, when we start index.py in terminal:

$ python index.py
 * Running on http://127.0.0.1:5000/

we notice within the browser:

flask-proper

Meanwhile, terminal outputs the following shortly after the browser attempts access:

$ python index.py
 * Running on http://127.0.0.1:5000/
127.0.0.1 - - [22/Aug/2014 09:18:25] "GET /tests/python/index.py HTTP/1.1" 200 -

All other pages renders a 404 error within the browser:

flask-404

and, within the terminal:

$ python index.py
 * Running on http://127.0.0.1:5000/
127.0.0.1 - - [22/Aug/2014 09:20:07] "GET /tests/php HTTP/1.1" 404 -

jeff1evesque added a commit that referenced this issue Aug 22, 2014
jeff1evesque added a commit that referenced this issue Aug 22, 2014
@jeff1evesque
Copy link
Owner Author

We will now try to fetch POST data from form.html into flask_form.py.

@jeff1evesque
Copy link
Owner Author

Now, we start the flask flask_form.py script:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/

Then, we visit the form.html:

form-html

which redirects us to the following page upon submission:

404-error

The terminal page dedicated to flask_form.py remains unchanged:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/

So, manually type http://localhost:5000/machine-learning/tests/python/flask_form.py into the address bar in the browser:

image

The terminal page dedicated to flask_form.py changes respectively:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
127.0.0.1 - - [22/Aug/2014 18:04:34] "GET /machine-learning/tests/python/flask_form.py HTTP/1.1" 404 -

@jeff1evesque jeff1evesque changed the title Research Python Web Framework Send form POST data to Python Flask Aug 23, 2014
@jeff1evesque jeff1evesque changed the title Send form POST data to Python Flask Flask: Send form POST data to Python script Aug 23, 2014
@jeff1evesque
Copy link
Owner Author

We need to implement an interface that will allow the communication between our webserver (apache2), and our python script, flask_form.py.

The first step is to download mod_wsgi:

$ sudo apt-get install libapache2-mod-wsgi

Next, we define a .wsgi file:

$ cd /var/www/html/machine-learning/tests/python/
$ sudo pico flask_form.wsgi

with the following content:

## flask_form.wsgi
#  This file will import the main 'flask' application object, and
#  execute it within the WSGI Apache module.

import sys
from flask_form import app as application

Note: WSGI (Web Server Gateway Interface) is an interface between web servers and web apps for python. Mod_wsgi is an Apache HTTP server mod that enables Apache to serve Flask applications.

Then, we find the vhosts:

$ apache2ctl -S
VirtualHost configuration:
*:80                   localhost (/etc/apache2/sites-enabled/000-default.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl 
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: ENABLE_USR_LIB_CGI_BIN
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used

so, that we can adjust the vhost configurations for Apache2:

$ sudo pico /etc/apache2/sites-enabled/000-default.conf

by adding the following to the top of the file:

# VirtualHost used for Python Flask (edited by JL)
<VirtualHost *>
    ServerName localhost

    WSGIDaemonProcess yourapplication user=nobody group=www-data threads=5
    WSGIScriptAlias /machine-learning/tests/python/ /var/www/html/machine-learning/tests/python/flask_form.wsgi

    <Directory /var/www/html/machine-learning/tests/python/>
        WSGIProcessGroup flask_form     
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>
...

Note: We used the suggested nobody user, and www-data as the group value for the virtualhost configuration. Keep in mind, that the vhost result above uses www-data for both user, and group.

Note: In the above case, we have a single vhost, specifically /etc/apache2/sites-enabled/000-default.conf.

After making the above configurations, we are still receiving the 404 error within terminal, as well as the same Not Found message within the browser window. When we change user=nobody to user=www-data we get a 500 error instead. However, the error log located in /var/log/apache2/error.log does not produce any errors (since apache2 restart / boot).

IRC #httpd (08/23/14 ~ 1:30pm EST):

jeffreylevesque: I have to make some apache2 configuration - http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/#configuring-apache. Should I make this within an '.htaccess' within the same directory that contains my script?

thumbs: fajita: htaccess use
fajita: if you have access to httpd.conf, there is no reason to use htaccess. You can get a significant performance boost disabling htaccess altogether with AllowOverride None. Your RewriteRules will be simpler too!

jeffreylevesque: I downloaded LAMP (php) using Ubuntu Server 14.04. I will try to locate the httpd.conf, and insert the code in that file

thumbs: jeffreylevesque: just edit your vhost.
thumbs: (if you have one)
thumbs: jeffreylevesque: apachectl -S shows your current vhosts.

IRC #python (08/23/14 ~ 11:30am EST):

jeffreylevesque: how do i run flask in apache2?

therealfakemoot: jeffreylevesque: you'd configure mod_wsgi to invoke your flask application

rivarun: jeffreylevesque: http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/

jeffreylevesque: rivarun, this will allow me to run flask on a subdirectory?

rivarun: jeffreylevesque: "on a subdirectory"? you mean as a distinct script? like http://your_server/your_app/ ?

jeffreylevesque: rivarun, yes sir. http://my_server/my_app/app.py

rivarun: jeffreylevesque: you would never point to the file itself, so the app.py part will definitely not be there. you should be able to do this with apache, yes, but i don't use it
rivarun: jeffreylevesque: there's #pocoo for flask specific questions

@jeff1evesque
Copy link
Owner Author

We removed all errors in /var/log/apache2/error.log, then restarted apache2:

$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2                                         [ OK ] 

Now, the following errors are apparent in /var/log/apache2/error.log:

Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
[Sat Aug 23 15:51:29.078796 2014] [core:notice] [pid 4250] AH00051: child pid 4254 exit signal Aborted (6), possible coredump in /etc/apache2
[Sat Aug 23 15:51:29.078906 2014] [core:notice] [pid 4250] AH00051: child pid 4255 exit signal Aborted (6), possible coredump in /etc/apache2
[Sat Aug 23 15:51:29.078929 2014] [core:notice] [pid 4250] AH00051: child pid 4257 exit signal Aborted (6), possible coredump in /etc/apache2
[Sat Aug 23 15:51:29.078948 2014] [core:notice] [pid 4250] AH00051: child pid 4258 exit signal Aborted (6), possible coredump in /etc/apache2
[Sat Aug 23 15:51:30.080653 2014] [core:notice] [pid 4250] AH00051: child pid 4256 exit signal Aborted (6), possible coredump in /etc/apache2
[Sat Aug 23 15:51:30.080810 2014] [mpm_prefork:notice] [pid 4250] AH00169: caught SIGTERM, shutting down
[Sat Aug 23 15:51:31.044482 2014] [:notice] [pid 4311] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads.
[Sat Aug 23 15:51:31.044565 2014] [:notice] [pid 4311] mod_python: using mutex_directory /tmp 
[Sat Aug 23 15:51:31.065271 2014] [mpm_prefork:notice] [pid 4311] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.3 mod_python/3.3.1 Python/2.7.6 mod_wsgi/3.4 configured -- resuming normal operations
[Sat Aug 23 15:51:31.065331 2014] [core:notice] [pid 4311] AH00094: Command line: '/usr/sbin/apache2'
[Sat Aug 23 15:51:31.066918 2014] [:alert] [pid 4315] (2)No such file or directory: mod_wsgi (pid=4315): Unable to change working directory to '/nonexistent'.

The repeating error:

Fatal Python error: PyEval_AcquireThread: NULL new thread state

indicates mod_wsgi is an incompatible version with python:

mod_wsgi is compiled for a different Python version and/or a different Python installation than the Python virtual environment or Python installation it is trying to use at runtime.

Note: the last line of error can be eliminated by removing what we had earlier added to etc/apache2/sites-enabled/000-default.conf:

# VirtualHost used for Python Flask (edited by JL)
<VirtualHost *>
    ServerName localhost

    WSGIDaemonProcess yourapplication user=nobody group=www-data threads=5
    WSGIScriptAlias /machine-learning/tests/python/ /var/www/html/machine-learning/tests/python/flask_form.wsgi

    <Directory /var/www/html/machine-learning/tests/python/>
        WSGIProcessGroup flask_form     
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>
...

At Apache2 reboot, we receive the following error messages:

[Sat Aug 23 20:36:03.344749 2014] [:notice] [pid 1567] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads.
[Sat Aug 23 20:36:03.351211 2014] [:notice] [pid 1567] mod_python: using mutex_directory /tmp 
[Sat Aug 23 20:36:03.571755 2014] [:alert] [pid 1654] (2)No such file or directory: mod_wsgi (pid=1654): Unable to change working directory to '/nonexistent'.
[Sat Aug 23 20:36:03.738904 2014] [mpm_prefork:notice] [pid 1567] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.3 mod_python/3.3.1 Python/2.7.6 mod_wsgi/3.4 configured -- resuming normal operations
[Sat Aug 23 20:36:03.738952 2014] [core:notice] [pid 1567] AH00094: Command line: '/usr/sbin/apache2'

@jeff1evesque
Copy link
Owner Author

We changed our earlier VirtualHost:

$ sudo pico /etc/apache2/sites-enabled/000-default.conf

to the following:

# VirtualHost used for Python Flask (edited by JL)
<VirtualHost *>
    ServerName localhost
    DocumentRoot /var/www/html/machine-learning/

    WSGIDaemonProcess flask_form user=www-data group=www-data threads=5
    WSGIScriptAlias /machine-learning/tests/python/ /var/www/html/machine-learn$

    <Directory /var/www/html/machine-learning/tests/python/>
        WSGIProcessGroup flask_form
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

...

Now, we run flask_form.py:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/

and, visit http://localhost:5000/machine-learning/tests/python/flask_form.py in the browser:

internal-error

which produces the following message within the terminal console running flask_form.py:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
127.0.0.1 - - [24/Aug/2014 08:41:54] "GET /machine-learning/tests/python/flask_form.py HTTP/1.1" 500 -

We notice /var/log/apache2/error.log has not changed since apache2 boot:

[Sun Aug 24 07:25:53.420332 2014] [:notice] [pid 1582] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads.
[Sun Aug 24 07:25:53.420480 2014] [:notice] [pid 1582] mod_python: using mutex_directory /tmp 
[Sun Aug 24 07:25:53.466327 2014] [mpm_prefork:notice] [pid 1582] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.3 mod_python/3.3.1 Python/2.7.6 mod_wsgi/3.4 configured -- resuming normal operations
[Sun Aug 24 07:25:53.466400 2014] [core:notice] [pid 1582] AH00094: Command line: '/usr/sbin/apache2'
[Sun Aug 24 07:25:53.466470 2014] [core:notice] [pid 1582] AH00051: child pid 1648 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 07:25:53.466975 2014] [core:notice] [pid 1582] AH00051: child pid 1653 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 07:25:53.467600 2014] [core:notice] [pid 1582] AH00051: child pid 1662 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 07:25:53.468159 2014] [core:notice] [pid 1582] AH00051: child pid 1663 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 07:25:53.468776 2014] [core:notice] [pid 1582] AH00051: child pid 1668 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 07:25:53.469644 2014] [:alert] [pid 3522] (2)No such file or directory: mod_wsgi (pid=3522): Unable to change working directory to '/nonexistent'.

@jeff1evesque
Copy link
Owner Author

ace23b9: we allow flask to write its errors in /var/log/apache2/error.log.

Now, restart apache2:

$ sudo /etc/init.d/apache2 restart
[sudo] password for jeffrey: 
 * Restarting web server apache2                                         [ OK ]

Then, start flask_form.py in terminal:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

Access the page in the browser:

flask-name-error

The terminal console running flask_form.py outputs the following:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [24/Aug/2014 09:05:56] "GET /machine-learning/tests/python/flask_form.py HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1917, in __call__
    return self.wsgi_app(environ, start_response)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1901, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1479, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1898, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1553, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1457, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1551, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1537, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/www/html/machine-learning/tests/python/flask_form.py", line 13, in parse_request
    temp = request.form['svm_session']
NameError: global name 'request' is not defined
127.0.0.1 - - [24/Aug/2014 09:05:56] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Aug/2014 09:05:56] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [24/Aug/2014 09:05:56] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [24/Aug/2014 09:05:56] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [24/Aug/2014 09:05:56] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -
127.0.0.1 - - [24/Aug/2014 09:05:56] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

And, /var/log/apache2/error.log adds the following lines:

...
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
Fatal Python error: PyEval_AcquireThread: NULL new thread state
[Sun Aug 24 09:03:38.847728 2014] [core:notice] [pid 1582] AH00051: child pid 3523 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 09:03:38.848458 2014] [core:notice] [pid 1582] AH00051: child pid 3524 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 09:03:38.848640 2014] [core:notice] [pid 1582] AH00051: child pid 3525 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 09:03:38.848803 2014] [core:notice] [pid 1582] AH00051: child pid 3526 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 09:03:38.848891 2014] [core:notice] [pid 1582] AH00051: child pid 3527 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 09:03:38.848978 2014] [core:notice] [pid 1582] AH00051: child pid 3826 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 09:03:38.849143 2014] [core:notice] [pid 1582] AH00051: child pid 3827 exit signal Aborted (6), possible coredump in /etc/apache2
[Sun Aug 24 09:03:38.849364 2014] [mpm_prefork:notice] [pid 1582] AH00169: caught SIGTERM, shutting down
[Sun Aug 24 09:03:39.768313 2014] [:notice] [pid 4527] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads.
[Sun Aug 24 09:03:39.768415 2014] [:notice] [pid 4527] mod_python: using mutex_directory /tmp 
[Sun Aug 24 09:03:39.846258 2014] [mpm_prefork:notice] [pid 4527] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.3 mod_python/3.3.1 Python/2.7.6 mod_wsgi/3.4 configured -- resuming normal operations
[Sun Aug 24 09:03:39.846317 2014] [core:notice] [pid 4527] AH00094: Command line: '/usr/sbin/apache2'

@jeff1evesque
Copy link
Owner Author

We've switched our environment from a temporary working environment, to a dual boot into Ubuntu Server 14.04. Upon sudo reboot in the Ubuntu partition, we notice the following messages within /var/log/apache2/error.log:

[Tue Aug 26 09:42:34.889508 2014] [mpm_prefork:notice] [pid 1293] AH00169: caught SIGTERM, shutting down
[Tue Aug 26 09:43:17.925119 2014] [mpm_prefork:notice] [pid 1298] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.3 configured -- resuming normal operations
[Tue Aug 26 09:43:17.972241 2014] [core:notice] [pid 1298] AH00094: Command line: '/usr/sbin/apache2'

Next, we start flask_form.py:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

and, notice /var/log/apache2/error.log has not changed. So, we proceed opening flask_form.py within the browser:

flask-name_error

Next, we revisit /var/log/apache2/error.log, and still notice no new error messages have been contributed to the file. So, we proceed by looking at the terminal tab running flask_form.py, and notice the following output:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [26/Aug/2014 09:53:56] "GET /machine-learning/tests/python/flask_form.py HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1917, in __call__
    return self.wsgi_app(environ, start_response)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1901, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1479, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1898, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1553, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1457, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1551, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1537, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/www/html/machine-learning/tests/python/flask_form.py", line 13, in parse_request
    temp = request.form['svm_session']
NameError: global name 'request' is not defined
127.0.0.1 - - [26/Aug/2014 09:53:57] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 09:53:57] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 09:53:57] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 09:53:57] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 09:53:57] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 09:53:57] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

@jeff1evesque
Copy link
Owner Author

640a4e3: We've updated flask_form.py to import request. So, we restart flask_form.py within terminal:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

Then, we check /var/log/apache2/error.log, and notice no new log messages. So, we proceed opening flask_form.py within the browser:

flask-bad-request

Next, we revisit /var/log/apache2/error.log, and still notice no new error messages have been contributed to the file. So, we proceed by looking at the terminal tab running flask_form.py, and notice the following output:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [26/Aug/2014 10:22:19] "GET /machine-learning/tests/python/flask_form.py HTTP/1.1" 400 -

@jeff1evesque
Copy link
Owner Author

We make sure to install mod_wsgi as we did earlier, and restart apache2:

$ sudo apt-get install libapache2-mod-wsgi
$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2                                    [ OK ]

We check /var/log/apache2/error.log, and notice the additional error log messages:

[Tue Aug 26 10:30:56.825163 2014] [mpm_prefork:notice] [pid 1298] AH00169: caught SIGTERM, shutting down
[Tue Aug 26 10:30:58.000576 2014] [mpm_prefork:notice] [pid 5251] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.3 mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Tue Aug 26 10:30:58.000645 2014] [core:notice] [pid 5251] AH00094: Command line: '/usr/sbin/apache2'
[Tue Aug 26 10:31:47.288666 2014] [mpm_prefork:notice] [pid 5251] AH00169: caught SIGTERM, shutting down
[Tue Aug 26 10:31:48.366284 2014] [mpm_prefork:notice] [pid 5358] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.3 mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Tue Aug 26 10:31:48.366359 2014] [core:notice] [pid 5358] AH00094: Command line: '/usr/sbin/apache2'

Again, we restart flask_form.py within terminal:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

Then, we check /var/log/apache2/error.log, and notice no new log messages. So, we proceed opening flask_form.py within the browser:

flask-bad-request

Next, we revisit /var/log/apache2/error.log, and still notice no new error messages have been contributed to the file. So, we proceed by looking at the terminal tab running flask_form.py, and notice the following output:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [26/Aug/2014 10:38:06] "GET /machine-learning/tests/python/flask_form.py HTTP/1.1" 400 

@jeff1evesque
Copy link
Owner Author

We use form.html to submit POST data to flask_form.py, and ensure that flask_form.wsgi, along with our VirtualHost is configured as above:

sudo pico /etc/apache2/sites-enabled/000-default.conf

with the following:

# VirtualHost used for Python Flask (edited by JL)
<VirtualHost *>
    ServerName localhost
    DocumentRoot /var/www/html/machine-learning/

    WSGIDaemonProcess flask_form user=www-data group=www-data threads=5
    WSGIScriptAlias /machine-learning/tests/python/ /var/www/html/machine-learn$

    <Directory /var/www/html/machine-learning/tests/python/>
        WSGIProcessGroup flask_form
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

...

ac1c7f2: We add app.config['TRAP_HTTP_EXCEPTIONS'] = True in flask_form.py.

TRAP_HTTP_EXCEPTIONS

If this is set to True Flask will not execute the error handlers of HTTP exceptions but instead treat the exception like any other and bubble it through the exception stack. This is helpful for hairy debugging situations where you have to find out where an HTTP exception is coming from.

We check /var/log/apache2/error.log, and notice no additional error log messages. So, we restart flask_form.py:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

Then, proceed opening flask_form.py within the browser:

flask-routing_builderror

Next, we revisit /var/log/apache2/error.log, and still notice no new error messages have been contributed to the file. So, we proceed by looking at the terminal tab running flask_form.py, and notice the following output:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [26/Aug/2014 11:48:51] "GET /machine-learning/tests/python/flask_form.py HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1917, in __call__
    return self.wsgi_app(environ, start_response)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1901, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1479, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1898, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1553, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1457, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1551, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1537, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/www/html/machine-learning/tests/python/flask_form.py", line 16, in parse_request
    temp = request.form['svm_session']
  File "/usr/local/lib/python2.7/dist-packages/Werkzeug-0.9.6-py2.7.egg/werkzeug/datastructures.py", line 393, in __getitem__
    raise exceptions.BadRequestKeyError(key)
BadRequestKeyError: 400: Bad Request
127.0.0.1 - - [26/Aug/2014 11:48:51] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 11:48:51] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 11:48:51] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 11:48:51] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 11:48:51] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 11:48:51] "GET /machine-learning/tests/python/flask_form.py?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

However, our definitions in flask_form.py line 19:

...
  else:
    return redirect(url_for('/machine-learning/tests/python/form.html'))
...

should redirect all requests that are not POST to /machine-learning/tests/python/form.html. Instead the browser displayed the BuildError message.

@jeff1evesque
Copy link
Owner Author

9a9c212: we add conditional statements based on the HTTP method, and redirect to the form.html if it is not a POST method.

@jeff1evesque
Copy link
Owner Author

Now, we visit the form.html:

form-html

which redirects us to the following page upon submission:

404-error

The terminal page dedicated to flask_form.py remains unchanged:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/

and, no new log error messages have been added.

Then, we visit http://localhost:5000/machine-learning/tests/python/form.html:

flask_notfound

We notice the following messages in terminal:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [26/Aug/2014 21:12:27] "GET /machine-learning/tests/python/form.html HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1917, in __call__
    return self.wsgi_app(environ, start_response)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1901, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1479, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1898, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1553, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1457, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1551, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1529, in dispatch_request
    self.raise_routing_exception(req)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1512, in raise_routing_exception
    raise request.routing_exception
NotFound: 404: Not Found
127.0.0.1 - - [26/Aug/2014 21:12:27] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 21:12:27] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 21:12:27] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 21:12:27] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 21:12:27] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2014 21:12:27] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

However, we find no new error log messages in /var/log/apache2/error.log.

@jeff1evesque
Copy link
Owner Author

6507968: We properly implement redirect(). Now, all users that attempt to access http://localhost:5000/machine-learning/tests/python/flask_form.py, will be redirected to http://localhost:5000/machine-learning/tests/python/form.html. Although the redirection works, we are still supplied the werkzeug.exceptions.NotFound error:

form-nofound

Note: Earlier we copied an incomplete copy of the VirtualHost into /etc/apache2/sites-enabled/000-default.conf (notice the line beginning with WSGIScriptAlias):

# VirtualHost used for Python Flask (edited by JL)
<VirtualHost *>
    ServerName localhost
    DocumentRoot /var/www/html/machine-learning/

    WSGIDaemonProcess flask_form user=www-data group=www-data threads=5
    WSGIScriptAlias /machine-learning/tests/python/ /var/www/html/machine-learn$

    <Directory /var/www/html/machine-learning/tests/python/>
        WSGIProcessGroup flask_form
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

...

We remedy our typo / mistake by copying the following, instead:

# VirtualHost used for Python Flask (edited by JL)
<VirtualHost *>
    ServerName localhost

    WSGIDaemonProcess flask_form user=www-data group=www-data threads=5
    WSGIScriptAlias / /var/www/html/machine-learning/tests/python/flask_form.wsgi

    <Directory /var/www/html/machine-learning/tests/python/>
        WSGIProcessGroup flask_form
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

...

We are still supplied with the same NotFound browser message, and the terminal console running flask_form.py outputs the following:

$ python flask_form.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [27/Aug/2014 11:40:15] "GET /machine-learning/tests/python/flask_form.py HTTP/1.1" 302 -
127.0.0.1 - - [27/Aug/2014 11:40:15] "GET /machine-learning/tests/python/form.html HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1917, in __call__
    return self.wsgi_app(environ, start_response)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1901, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1479, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1898, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1553, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1457, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1551, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1529, in dispatch_request
    self.raise_routing_exception(req)
  File "/var/www/html/machine-learning/flask/flask/app.py", line 1512, in raise_routing_exception
    raise request.routing_exception
NotFound: 404: Not Found
127.0.0.1 - - [27/Aug/2014 11:40:15] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [27/Aug/2014 11:40:15] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [27/Aug/2014 11:40:15] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [27/Aug/2014 11:40:15] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [27/Aug/2014 11:40:15] "GET /machine-learning/tests/python/form.html?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -

@jeff1evesque
Copy link
Owner Author

We will simply have the the form POST data be sent to another php script. This php script will delegate, or pass relevant parameters to python (i.e. logic_loader.py).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant