Is there an easy way to receive Http POST request in python, like in php?

I am implementing a simple Android app, that needs to send data to a webserver. I currently use a HttpUrlConnection to send the data via a POST request to a php script on the server.
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
The values are received like simplified shown above. In the app I use the url, where the script is saved on the server.
Is there a simple way to get this done with python too? Or are there just some more complex solutions?
I could only find multiple ways of sending request with python or exchanging data within the webserver. But nothing worked for my project.
Answer
Solution:
In Python the easier way to getPOST
is to useFlask
and run it with built-in server.
script.py
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
value1 = request.form.get('value1')
value2 = request.form.get('value2')
return 'OK'
else:
return 'Use POST requests'
if __name__ == '__main__':
#app.debug = True
app.run(host='0.0.0.0', port=8000)
and run as
python script.py
and it will run server on port8000
so you can connecthttp://myserver:8000/
But if you already runApache
/nginx
withPHP
then this makes problem.
You can't run both servers on the same port80
. It would need to run all withApache
.
PHP
was created for web pages andApache
has preinstalled module to runPHP
(and to run PHP frameworks likeLaravel
) but usually it doesn't have module forPython
. It may install module to run Python or other module to run CGI or FastCGI (which means any executable script - Python, Perl, Bash, and even compiled C/C++) but this is very old method which needs different modules in Python - so rather it can't runFlask
- and probably nobody use it.Flask
,Django
,Bottle
is simpler to write code, has many built-in functions and code is cleaner and better organized - like in PHP frameworks.
Flask
and other Python web frameworks (likeDjango
,Bottle
) are created to run with WSGI severs likeGunicorn
. They use eventuallyApache
only asproxy-server
and to serve static files like images, CSS. So to use Flask/Django on external server you would have to find server which is configured forFlask
,Django
, etc. like PythonAnywhere
InPHP
you can mix all in one file - code, HTML, SQL - so it can make big mess and people created frameworks likeLaravel
to make it cleaner and better organized. InPython
you use at start framework so you would have to compareFlask
with PHP frameworks but not with pure PHP.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.