What technology is used to serve HTTP requests compatible with Python - python

I'm building an application on AWS and well, this world is new to me.
I expose the problem
I have experience with Apache / PHP, Apache is the one who helps me serve HTTP requests and PHP is the language of Backend.
The backend language that I am using in this new project is Python, but my question is, what is the technology that helps me serve the requests?
Can I install Apache / Python, or what would be the perfect duo?
I know this can have many variants depending on each experience and needs of the project, but honestly I am lost in what to install and what not.
Thanks for your guidance

Your best course of action if just learning how to do web programming in Python is to not worry about how you host it. Start out with one of the major Python web frameworks and work through their tutorials. The best options are:
Flask - http://flask.pocoo.org/
Django - https://www.djangoproject.com/
They will explain the basics of building your web application. They use a development server to host your application. Only once you understand programming basics, then worry about a production grade web server.

I would recommend to look at uWSGI:
https://uwsgi-docs.readthedocs.io/en/latest/WebServers.html
mod_wsgi for Apache:
https://modwsgi.readthedocs.io/en/develop/
For simple pages you still have CGI:
https://docs.python.org/3.6/library/cgi.html
There exists also mod_python for Apache, but it isn't modern way to embed Python interpreter within the webserver (don't use it, here only for information purposes!).
http://modpython.org/

Related

Python Web Server - mod_wsgi

I have been looking at setting up a web server to use Python and I have installed Apache 2.2.22 on Debian 7 Wheezy with mod_wsgi. I have gotten the initial page up and going and the Apache will display the contents of the wsgi file that I have in my directory.
However, I have been researching on how to deploy a Python application and I have to admin, I find some of it a little confusing. I am coming from a background in PHP where it is literally install what you need and you are up and running and PHP is processing the way it should be.
Is this the same with Python? I can't seem to get anything to process outside of the wsgi file that I have setup. I can't import anything from other files without the server throwing a "500" error. I have looked on Google and Bing to try to find an answer to this, but I can't seem to find anything, or don't know that what I have been looking at is the answer.
I really appreciate any help that you guys can offer.
Thanks in advance! (If I need to post any coding, I can do that, I just don't know what you guys would need, if anything, as far as coding examples for this...)
Python is different from PHP in that PHP executes your entire program separately for each hit to your website, whereas Python runs "worker processes" that stay resident in memory.
You need some sort of web framework to do this work for you (you could write your own, but using someone else's framework makes it much easier). Flask is an example of a light one; Django is an example of a very heavy one. Pick one and follow that framework's instructions, or look for tutorials for that framework. Since the frameworks differ, most practical documentation on handling web services with Python are focused around a framework instead of just around the language itself.
Nearly any python web framework will have a development server that you can run locally, so you don't need to worry about deploying yet. When you are ready to deploy, Apache will work, although it's usually easier and better to use Gunicorn or another python-specific webserver, and then if you need more webserver functionality, set up nginx or Apache as a reverse proxy. Apache is a very heavy application to use for nothing but wsgi functionality. You also have the option of deploying to a PaaS service like Heroku (free for development work, costs money for production applications) which will handle a lot of sysadmin work for you.
As an aside, if you're not using virtualenv to set up your Python environment, you should look into it. It will make it much easier to keep track of what you have installed, to install new packages, and to isolate an environment so you can work on multiple projects on the same computer.

Web framework for *not* creating websites? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Recommended python library/framework for local web app?
I am looking for a lightweight framework/webserver that will make it easy to develop web interfaces for some demon processes or some application that should have ideally be an desktop application.
Some examples
To organize photo/music collections, move between folders, edit meta data of those etc,
A simple to-do list/ Money manager/ Documents manager etc,
A log/textFile viewer
A page showing the status of my git repositories
I prefer server side to be python. I don't want to run a full webserver like apache. As this is going to be used similar to a desktop app, I dont need any security features that will prevent me from accessing file system or running any shell commands or accessing internet etc.,
Any suggestions?
Check bottle.py
Most frameworks will have built-in development servers, so you can still use django or some other. But if you are looking for something lightweight to do some quick project, bottle.py can be great
Werkzeug is a good choice. It's a web toolkit more than a web framework. It's very mature, and is the basis of Flask, a popular microframework.
I've developed a website using vanilla Werkzeug and it was really intuitive. It has an explicit feel to it. None of the RoR-style autoload-controller and folder logic, just a bunch of classes and modules that are really well-documented and useful.
As well as cherry.py is a lightweight framework with full server.
http://www.cherrypy.org/
CherryPy allows developers to build web applications in much
the same way they would build any other object-oriented Python
program. This results in smaller source code developed in less time.
Any Python web microframework running on any wsgi compliant Python lightweight web server will do. There are already questions on both here.

python website development [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to create a simple website, the server is gonna running on Amazon EC2 web service. But I haven't done web development before. It seems like I need to learn a lot of stuff, javascript, html, css, web framework, WSGI, apache web server, etc. And I am confused by some stuff that seems much the same.
So first question, Can anyone tell me what exactly do I need to set the website up. Is a web framework the only thing I need on the server side?
2th question, I am gonna use python on server side, there are really a bunch of web frameworks for python. What's the difference between apache http server and the built-in wsgi or http web servers in those web frameworks.
3th question, Is WSGI the best choice? Most web frameworks support WSGI, does it mean WSGI is built in the web frameworks? Or I need to include mod_wsgi or something alike.
Also, according to this benchmark of python WSGI servers, gevent and mod_wsgi show great performance with low memory footprint. They don't claim themselves as web frameworks, what's the difference between them and the web frameworks like cherrypy, web2py.
Thanks
Depends on your website - if you only need static content, you can use static site generators (Blogofile, Hyde to name a few written in Python) or, if your website serves dynamic content, you should use some kind of framework (Flask, Django, etc). With static site generators all you need on the server is the webserver that serves your content, but with dynamic sites you might need extra libraries for various things like database support, caching, etc.
The built-in webservers are usually meant for local development and are generally not suitable for production use. For latter use the specific apps like mod_wsgi (for Apache) or uwsgi (for Nginx) instead. But try to avoid mod_python!
WSGI stands for Web Server Gateway Interface and is a standard protocol used between web server and web application (or framework). WSGI seems to be currently the only standard choice, so go with it.
Also, according to this benchmark of python WSGI servers, gevent and
mod_wsgi show great performance with low memory footprint. They don't
claim themselves as web frameworks, what's the difference between them
and the web frameworks like cherrypy, web2py.
As said before, WSGI is just a way that web server talks with web application. CherryPy and web2py deal with templating, request and session handling and output this information via WSGI to the web server.
And if you worry about the speed, then don't worry about it too much.. :)
WSGI is an interface. It requires a container (e.g. mod_wsgi), an application (e.g. Django), and optional middleware. Applications can be built directly against WSGI, or they can be built on top of frameworks such as Django that handle the WSGI bit for you.
Don't use the built-in web server in the frameworks. They aren't usually designed for performance, and they usually don't integrate well with the non-WSGI parts of your server.
You will need the web server, Apache is one popular choice. You will probably also need to install Database, MySQL is free, easy and popular.
The most popular (and best in my opinion) python framework is Django.
Many run Django with apache using mod_wsgi. Frameworks usually only provide development server that is not suitable to be a real server.
The WSGI servers probably only offer request handling and entry points for writing the code, but frameworks offer much more (user handling, admin interface, ORM, templating, etc...).
This isn't exactly a programming question, since you're really just asking how to get started with web development, and there is no clear answer to this.
I can warmly recommend getting started with Django, simply because it is very well documented and has great tutorials. It also uses Python which means it is easier to understand other people's code.
Regardless of whether you choose Django or something else, just choose a web application development tutorial and run through it. Google for one (for Django you can ask on the very helpful #django IRC channel). That should give you a good basis with at least one working example which you understand, as well as a clear picture of what the different pieces of a running web application are and how they work together.
Afterwards you can branch off and learn many various relevant skills and technologies, preferably driven by your needs as you develop an actual web application of your own (even if just for learning purposes at first).
Good luck!

How to develop a simple web application with server-side Python

I am wondering how to go about implementing a web application with Python.
For example, the html pages would link to python code that would give it increased functionality and allow it to write to a database.
Kind of like how Reddit does it.
If you're looking for server side programming with databases and html templates etc, I think Django is great, along with Pyramid. However, I use Flask ( http://flask.pocoo.org/ ) for this since it is easy to use, learn and deploy even though it may not have as much support as the before mentioned 2 framework since it's just a microframework, using the Jinja2 templating engine, including a development test server with it's own debugger.
On the other hand, if you're going for client-side programming (i.e. in browser implementation ) You can look up .NET Ironpython or even Brython which uses python like javascript.
You might want to check out mod_wsgi or mod_python.
What Is mod_wsgi?
The aim of mod_wsgi is to implement a simple to use
Apache module which can host any Python application which supports the
Python WSGI interface. The module would be suitable for use in hosting
high performance production web sites, as well as your average self
managed personal sites running on web hosting services.
-
Current State of Mod_Python
Currently mod_python is not under active development. This does not
mean that it is "dead" as some people have claimed. It smiply means
that the code and the project are mature enough when very little is
required to maintain it.
This is a good article from the Python website:
http://docs.python.org/howto/webservers.html
Plain CGI is a good starting point to learn about server side scripting, but it is an outdated technology and gets difficult to maintain after certain level of complexity. I would think it is no longer used in industrial-grade web server anymore. Plus you have to setup a web server and then install some module to interpret python script (like Apache with mod_python) just to get started.
I had some experience with Django (https://www.djangoproject.com/) and found it fairly easy to get started with since they come with development test server. All you need to have is a Python interpreter + Django and you can get up-and-running quickly and worry about the deployment setup later. They have pretty good documentation for beginner as well.
We have never used Python for a web site without a framework. In our case that is Django. In other words, we do not use Python for our web sites the way Perl can be used, just having Apache run a Perl script.
The recommendations you have received about Django are sound. If you go the Django route, Graham Dumpleton and the modwsgi Google group were very helpful to me. I could
not have gotten mod_wsgi deployed on Red Hat Enterprise 5 64-bit without Graham's help.
Whether you choose Django or "straight" Python, you will need to become familiar with mod_wsgi.
Good luck in quantum time, which means by now, I hope this all worked out for you.

How to run python scripts?

I am a PHP developer and am I want to use python for a project. What is the most common and simplest way to run python script on a server like apache or lighttpd? I am having trouble understanding how server languages that are not PHP or ASP run on servers.
In Python we have something called WSGI. But I believe this is too much for you right now. Grab a Python web framework, like Django. It comes with a web server and with good documentation on how to deploy it later. Play with it and things will start to clear up.
The longer version is that Python is a general purpose language - it's not designed for the web like PHP is. So you need a bit of work the get it to do web stuff and we already have some good frameworks for that (Django is the easiest to start with, so that is why I'm recommending it to you).
In general you should understand how the web works. It uses HTTP as a protocol for communication, which is build on top of the TCP stack, so a web application is just a server, that uses sockets (PHP has them as well as Python) and understands HTTP. Python comes with one build in - the SimpleHTTPServer, but it is not very good for production uses (it's great for development, though). This is why there are things like mod_wsgi (Python specific), FastCGI (general purpouse). Those things are basically ways for a real, production grade, web server (Apache, nginx) to talk to our python app and feed it the HTTP they get.
You can install gunicorn a Pure Python WSGI HTTP Server for UNIX. http://gunicorn.org/.
Typically, Apache is used with mod_python. See this mod_python tutorial

Categories