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
Comments
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):
|
b49b1d3: After installing flask, we changed ## 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
we notice within the browser: Meanwhile, terminal outputs the following shortly after the browser attempts access:
All other pages renders a 404 error within the browser: and, within the terminal:
|
We will now try to fetch POST data from |
Now, we start the flask
Then, we visit the which redirects us to the following page upon submission: The terminal page dedicated to
So, manually type The terminal page dedicated to
|
We need to implement an interface that will allow the communication between our webserver (apache2), and our python script, The first step is to download
Next, we define a .wsgi file:
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:
so, that we can adjust the vhost configurations for Apache2:
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 Note: In the above case, we have a single vhost, specifically 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 IRC #httpd (08/23/14 ~ 1:30pm EST):
IRC #python (08/23/14 ~ 11:30am EST):
|
We removed all errors in
Now, the following errors are apparent in
The repeating error:
indicates
Note: the last line of error can be eliminated by removing what we had earlier added to # 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:
|
We changed our earlier VirtualHost:
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
and, visit which produces the following message within the terminal console running
We notice
|
ace23b9: we allow flask to write its errors in Now, restart apache2:
Then, start
Access the page in the browser: The terminal console running
And,
|
We've switched our environment from a temporary working environment, to a dual boot into Ubuntu Server 14.04. Upon
Next, we start
and, notice Next, we revisit
|
640a4e3: We've updated
Then, we check Next, we revisit
|
We make sure to install
We check
Again, we restart
Then, we check Next, we revisit
|
We use
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
We check
Then, proceed opening Next, we revisit
However, our definitions in ...
else:
return redirect(url_for('/machine-learning/tests/python/form.html'))
... should redirect all requests that are not POST to |
9a9c212: we add conditional statements based on the HTTP method, and redirect to the |
Now, we visit the which redirects us to the following page upon submission: The terminal page dedicated to
and, no new log error messages have been added. Then, we visit We notice the following messages in terminal:
However, we find no new error log messages in |
6507968: We properly implement Note: Earlier we copied an incomplete copy of the VirtualHost into # 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
|
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. |
#20: Temporarily Explore Python Flask
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.The text was updated successfully, but these errors were encountered: