Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python CGI
#1
I have a pretty simple program that generates data based on a query.  I want to use it in a website and can't figure out how to implement it.  Basically how can you use python programs in websites?  I saw some information about CGIs but they seem insecure.
#2
It is not really my area of knowledge, and also from your post I am not entirely sure what exactly you are after.
Nonetheless, I believe Django + uWSGI is something worth looking up. Less recommended alternative to uWSGI is Gunicorn.
#3
I want to make a website where the user makes a query, python does some calculations and then presents the user with results.
#4
These are two different things. A query can be sent by anything. A website, an application, even curl:
$ curl http://example.com/user -d "first_name=Donald&last_name=Trump&[email protected]"

On the other side is a process which listens to a port for queries and if it receives one, may respond back or may not. This can be done in many ways. Socket module at least is always here but is a pain to invent the wheel one more time. There are a bunch of web frameworks and every one of them can save your time and nerves.

Here are few of them: bottle.py, Hug, Flask, Django ( too much for simple tasks ), Pyramid, Twisted, Tornado

I should recommend bottle.py, Flask or Hug. You can see how to use Flask for example here: https://python-forum.io/Forum-Web-Tutorials. It's a good start. See the other two too.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
#5
Flask is best suited for your task,is the most mature of the Microframeworks and the highest rated of all Python web-framework on Github 26k.
CGI is dead for all usage in Python after WSGI(an all Python solution).

The way this work for Flask or Django is that they have build in web-servers.
So you can build and test all local,when happy and want to deploy(share with the world).
Now you think of Hosts and deploy options.

Good hosts for Python Digital Ocean, Heroku ,PythonAnywhere, AWS Lambda.
When you deploy you don't use the build in web-server anymore.
Eg for Digital Ocean i always use Gunicorn with Ngix.

Easier option are PythonAnywhere and AWS Lambda they to all deploy stuff for you.
So eg PythonAnywhere is running uWSGI with Ngix but they to all setup.
Same with AWS Lambda(serverless web applications)
Quote:all with zero administration.
Just upload your code and Lambda takes care of everything required to run and scale your code with high availability
#6
Quote:Eg for Digital Ocean i always use Gunicorn with Ngix.

deploy flask on LAMP (linux, apache, mysql, php) 
https://www.digitalocean.com/community/t...ubuntu-vps
Recommended Tutorials:
#7
Quote:deploy flask on LAMP (linux, apache, mysql, php)
That's something i would not choose at all,PHP should not be in the picture in at all  Dodgy 
Ngix better and easier to setup,Apache not for me.
Quote:NGINX was written specifically to address the performance limitations of Apache web servers.
MySQL not a favorite Undecided
#8
Thanks for all the replies.  I have a hosting service for the webpage, will I need another host for the code?  Any recommendations for resources to get a better big picture idea of how these kind of applications work?  I am not even sure what are the right questions.  Can I combine python and javascript?
#9
Quote:That's something i would not choose at all,PHP should not be in the picture in at all  [Image: dodgy.png] 

Ngix better and easier to setup,Apache not for me.
I mean if its what you have its what you have. LAMP setup is common. And what if LAMP was already setup? You wouldnt tear down your site just to use flask for a page or two?
Recommended Tutorials:
#10
(Mar-24-2017, 08:55 PM)metulburr Wrote: LAMP setup is common. And what if LAMP was already setup? You wouldnt tear down your site just to use flask for a page or two?
Can maybe make sense for that,but would not do it.
I did run LAMP for years in old Python days with CGI,
now i am happy that Python has moved on an we have a modern way(WSGI) to deal with web development.

Quote:I have a hosting service for the webpage, will I need another host for the code?
Maybe have to ask if host if can run Python code.

Quote:Can I combine python and javascript?
Sure no problem.
Short as possible.
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return '''\
        <!DOCTYPE html>
        <html>
          <body>
            <button onclick="myFunction()">Try it</button>
            <script>
              function myFunction() {
                  alert('Running JavaScript from Flask');
              }
            </script>
          </body>
        </html>'''

if __name__ == "__main__":
    app.run(debug=True)
In browser http://127.0.0.1:5000/


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020