I want to build a web application that stands completely by itself, apache not required. Is cherrypy a good solution, and can this be compiled with something like py2exe?
Python is a scripting language and is not usually compiled. What you are talking about is packaging your scripts into an exe (via p2exe), bundled with the relative modules and an interpreter.
This is possible with many scripts, including CherryPy, as p2exe basically sticks all your scripts together in one place, then executes it with the interpreter. This link will allow you to build your application into an exe. I would however recommend that you use pyinstaller instead, as I have found it to be much easier.
This question shows that you can daemonize CherryPy, and from the page of CherryPy ->
Your CherryPy powered web applications
are in fact stand-alone Python
applications embedding their own
multi-threaded web server. You can
deploy them anywhere you can run
Python applications. Apache is not
required
So yes you can deploy CherryPy, self-contained without Apache. CherryPy seems to be a fine solution.
You are basically describing web2py.
Related
Yesterday I have found information about G-WAN server.
I have searched many websites but I couldn't discover how to run frameworks like Web2Py which is more complex then simple hello.py file.
Is there an easy way to do this?
Maybe uWSGI support?
G-WAN supports the CGI interface, so the CGI environment variables are already there. Usually, Web frameworks are built over this layer.
If uWSGI provides more environment variables that are needed for your project then please drop us a line at gwan.com to explain more precisely what you need.
I am creating a Python application that uses multiple third party libraries. Since the libraries are installed on my computer, the script runs fine. However, how can I alter my script so that it will run on any computer (all major OS), even if the computer does not have the third party Python libraries installed?
By your comment:
I want the script to stay a python script if at all possible so that
it can be run on any device and run through a webpage
It appears you want some way to host a python program online.
To do this, you need:
To know how to write Python that serves a website (see Django, Flask, CherryPy, etc...)
Some way to deploy said application to the web. An easy, free (<-- this is the keyword) way to deploy Python web apps is through using Heroku or some other free hosting site. Or you could always pay for hosting or host it yourself.
For very simple, internal web-apps using ASP I was able to just switch IIS 'on' and then write some ASP scripts in the www directory that would start working immediately.
Is there an equivalent webserver app for Python scripts that I can run that will automatically start serving dynamic pages (python scripts) in a certain folder (with virtually no configuration)?
Solutions I've already found are either too limited (e.g. SimpleHTTPRequestHandler doesn't serve dynamic content) or require configuring the script that does the serving.
There's always CGI. Add a script mapping of .py to "C:\Python27\python.exe" -u "%s" then drop .py files in a folder and IIS will execute them.
I'd not generally recommend it for real work—in the longer term you would definitely want to write apps to WSGI, and then deploy them through any number of interfaces including CGI—but it can be handy for quick prototyping.
For development or just to play around, here's an example using the standard Python library that I have used to help friend who wanted to get a basic CGI server up and running. It will serve python scripts from cgi-bin and files from the root folder. I'm not near a Windows computer at the moment to make sure that this still works. This also assumes Python2.x. Python 3.x has this, it's just not named the same.
Make a directory on your harddrive with a cgi-bin folder in it (Ex. "C:\server\cgi-bin")
In a command window, navigate to "C:\server" directory
Type the following assuming you've installed python 2.7 in C:\Python27:
"c:\python27\python.exe -m CGIHTTPServer"
You should get a message like "Serving HTTP on 0.0.0.0 port 8000"
Linux is the same - "python -m CGIHTTPServer" in a directory with a cgi-bin/ in it.
WSGI setups are fairly easy to get started, but in no anyway turn key. django MVC has a simple built in development server if you plan on using a more comprehensive framework.
My limited experience with Python web frameworks has taught me that most go to one extreme or the other: Django on one end is a full-stack MVC framework, that will do pretty much everything for you. On the other end, there are Flask, web.py, CherryPy, etc., which do much less, but stay out of your way.
CherryPy, for example, not only comes with no ORM, and doesn't require MVC, but it doesn't even have a templating engine. So unless you use it with something like Cheetah, you can't write what would look like .asp at all.
We currently run a small shared hosting service for a couple of hundred small PHP sites on our servers. We'd like to offer Python support too, but from our initial research at least, a server restart seems to be required after each source code change.
Is this really the case? If so, we're just not going to be able to offer Python hosting support. Giving our clients the ability to upload files is easy, but we can't have them restart the (shared) server process!
PHP is easy -- you upload a new version of a file, the new version is run.
I've a lot of respect for the Python language and community, so find it hard to believe that it really requires such a crazy process to update a site's code. Please tell me I'm wrong! :-)
Python is a compiled language; the compiled byte code is cached by the Python process for later use, to improve performance. PHP, by default, is interpreted. It's a tradeoff between usability and speed.
If you're using a standard WSGI module, such as Apache's mod_wsgi, then you don't have to restart the server -- just touch the .wsgi file and the code will be reloaded. If you're using some weird server which doesn't support WSGI, you're sort of on your own usability-wise.
Depends on how you deploy the Python application. If it is as a pure Python CGI script, no restarts are necessary (not advised at all though, because it will be super slow). If you are using modwsgi in Apache, there are valid ways of reloading the source. modpython apparently has some support and accompanying issues for module reloading.
There are ways other than Apache to host Python application, including the CherryPy server, Paste Server, Zope, Twisted, and Tornado.
However, unless you have a specific reason not to use it (an since you are coming from presumably an Apache/PHP shop), I would highly recommed mod_wsgi on Apache. I know that Django recommends modwsgi on Apache and most of the other major Python frameworks will work on modwsgi.
Is this really the case?
It Depends. Code reloading is highly specific to the hosting solution. Most servers provide some way to automatically reload the WSGI script itself, but there's no standardisation; indeed, the question of how a WSGI Application object is connected to a web server at all differs widely across varying hosting environments. (You can just about make a single script file that works as deployment glue for CGI, mod_wsgi, passenger and ISAPI_WSGI, but it's not wholly trivial.)
What Python really struggles with, though, is module reloading. Which is problematic for WSGI applications because any non-trivial webapp will be encapsulating its functionality into modules and packages rather than simple standalone scripts. It turns out reloading modules is quite tricky, because if you reload() them one by one they can easily end up with bad references to old versions. Ideally the way forward would be to reload the whole Python interpreter when any file is updated, but in practice it seems some C extensions seem not to like this so it isn't generally done.
There are workarounds to reload a group of modules at once which can reliably update an application when one of its modules is touched. I use a deployment module that does this (which I haven't got around to publishing, but can chuck you a copy if you're interested) and it works great for my own webapps. But you do need a little discipline to make sure you don't accidentally start leaving references to your old modules' objects in other modules you aren't reloading; if you're talking loads of sites written by third parties whose code may be leaky, this might not be ideal.
In that case you might want to look at something like running mod_wsgi in daemon mode with an application group for each party and process-level reloading, and touch the WSGI script file when you've updated any of the modules.
You're right to complain; this (and many other WSGI deployment issues) could do with some standardisation help.
How do I deploy a Python project to a webserver that supports Python via CGI? I'm well versed in PHP, but do not understand CGI's relation to Python in the deployment process.
Any resource links are appreciated.
The web host in question is GoDaddy.
Generally, we use mod_wsgi to make a Python application respond to CGI.
PHP has a special role -- the language runtime IS a CGI application.
Python does not have this special role. Python -- by default -- is not a CGI application. It requires a piece of glue to play well with Apache. mod_wsgi is this glue.
To actually answer your question about deploying python as CGI (while it does not make a lot of sense on a high activity system - there are occasions where it does the job just fine) you just make sure that your files are executable, have the correct extension and then follow this tutorial. It is what I used to learn from.
EDIT: To be clear - I recommend that you look into Django to deploy web based python applications.