How to run python scripts? - python

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

Related

What technology is used to serve HTTP requests compatible with 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/

Simple Python server setup

I am trying to learn python (coming from PHP), and want to set up the simplest web server so I can start coding.
I found the integrated HTTP server, so I figured it should be the easiest way.
root#ubuntu:/var/py# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
The webserver is working, accessing http://test.dev:8000/test.py (thanks /etc/hosts) works - but shows me the contents of the file ( print('Hello world!'); ), without interpreting it.
How to properly set up the server / interpretor ?
Python is for writing programs in general, not only web-sites.
SimpleHTTPServer is really just a trivial HTTP server, it serves files. It doesn't try to interpret code.
If you want to do web-development with Python, you should be using a web framework. web.py is a good choice, you can check its tutorial. Another option is Django, which also has a brilliant tutorial.
Both of them have simple development servers built in, so you'll be able to start experimenting easily.
If you are just starting out with python I would recommend you start with scripts that can be run from the console using python interpreter. (eg: python run1.py)
Once you have mastered the basics, you can move onto web programming. (I am guessing that you want to try web programming since you mention a web server.) In this case, you have multiple options (all of which work with Apache):
Django framework : Really good framework, has a built-in webserver and has fantastic documentation (http://www.djangobook.com/en/2.0/index.html).
You need to know python basics first
WSGI: https://code.google.com/p/modwsgi/ : Apache module for running python code

Python self contained web application and server?

What is a good and easy way to distribute a web application and server bundled together, python way?
So I can say to a user "Here take this tar/whatever, unpack it and run blahblah.py" and blahblah.py will run a http/wsgi server and serve my application?
Im looking for a stable production-ready multi-threaded wsgi-server with which I can bundle my app, without the need for nginx or other "frontends" or having to deal with any configuration.
CherryPy can act as a WSGI container.
You might find this discussion on reddit informative.
cherrypy is the easiest one to use, django is feature rich and tornado is more advanced with asynchrounous web server(in my opinion it is better than multithreaded web server).
For what you want, django is best suitable for you IMO.

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.

Can I write Python web application for Windows and Linux platforms at the same time?

Can I write web application that I can host on Windows(IIS web server) and Linux (Apache or lighttpd) without any changes?
CGI? Maybe something new? WSGI | FastCGI ?
Yes you can. But you can also use apache on windows. If you go the IIS way there's only CGI and it's pretty hard to set up. You can also use python based server like CherryPy which is pretty good and will work on all platforms with python.
Some frameworks like django support both CGI and WSGI, so you don't have to worry about the details of WSGI or CGI much.
If you ask me, WSGI is the future for python web apps.
web.py includes a server... It will do the trick for small jobs.
By the way, Apache works on windows.
Yes, if you use CGI, FastCGI or depending on your framework, even a self-contained web server (so IIS and Apache would be a reverse-proxy) then that would all work.
The difference will be the configuration of the OS-specific servers, and also your Python environment on each OS. So you may find yourself doing a small bit of work at the beginning to make sure your paths are right, etc.
A rather big Python based web framework is ZOPE.
Zope is an open source application server for building content management systems, intranets, portals, and custom applications. The Zope community consists of hundreds of companies and thousands of developers all over the world, working on building the platform and Zope applications. Zope is written in Python, a highly-productive, object-oriented scripting language
ZOPE is available on Linux and Windows, and you can use Python to write your Zope Web Apps (it includes a simpler templating system, too).
consider also the possibility of using web2Py, or XML-RPC implementation, or Twisted...
Writing python web apps is a topic on itself, but I would say that by default, it will be portable on multiple servers / platforms.
When developping python web applications, you will often use frameworks that provide their own web server. For performance reasons, you might want to place it behind apache, but it is not even necessary, however, you might get a performance boost by placing it behind an apache server.
Some of the most popular frameworks for web python are : Plone, Zope, CherryPy and TurboGears, only to name a few.
Under apache, you could also use python server pages through mod_python, and since apache runs on windows too, this would aslo be portable.

Categories