Beginner Python question about making a web app [closed] - python

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 3 years ago.
Improve this question
I am completely new to Python-- never used it before today. I am interested in devloping Python applications for the web. I would like to check to see if my web server supports WSGI or running python apps in some way.
Let's say I have a .py file that prints "Hello world!". How can I test to see if my server supports processing this file?
FYI, this is a Mac OS X server 10.5. So I know Python is installed (It's installed on Mac OS X by default), but I don't know if it's set up to process .py files server-side and return the results.
BTW, I'm coming from a PHP background, so this is a bit foreign to me. I've looked at the python docs re: wgsi, cgi, etc. but since I haven't done anything concrete yet, it's not quite making sense.

A very basic WSGI application can look as follows:
def application(environ, start_response):
start_response('200 OK', [('content-type', 'text/html')])
return ['Hello world!']
Unfortunately, if you put this into helloworld.py on the server and then browse to URL/helloworld.py you will most likely see the code.
In general you need to add very specific configuration options to the server (or to a server configuration file) to get it to serve your python 'application' correctly. Given you are using mod_wsgi on Apache 2, a configuration could look as follows:
<VirtualHost *>
ServerName example.com
WSGIScriptAlias /server/location/address /path/to/helloworld.py
</VirtualHost>
Where /server/location/address is the endpoint of the URL you have to browse to.
This is because python WSGI catches all URLs passed to it, and pushes them to the same entry point (your application method/class). And from the information received in the parameters, the application must decide what page to return.
Since this information is so application specific there 'should' be a way to configure it on the server, however I have yet to come across a web-hosting configuration panel that allows the configuration of Python applications. This generally means you have to contact the server administrators and have them configure it for you.
However, in general, when you sign up for hosting the company generally has a page where they tell you exactly what is supported on their servers (generally: php, mysql) and how much space and bandwidth you are allowed. Thus if they do not list it on their site, it is highly likely they will not support it.
To get around this you can instead buy a VPS (Virtual Private Server) and then configure it however you want.

If you are new to Python and Python web application development, then ignore all the hosting issues to begin with and don't start from scratch. Simply go get a full featured Python web framework such as Django or web2py and learn how to write Python web applications using their in built development web server. You will only cause yourself much pain by trying to solve distinct problem of production web hosting first.

Related

Python Restful API [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 3 years ago.
Improve this question
I have written a python script that will connect to the oracle database using cx_oracle and gets data and performs some action on it.
I want to expose this python script as a Restful API. In google, I read that using flask we can deploy a Python script as a Web service.
What I am not clear
The flask itself behaves like a server?
Can I deploy the python Webservice in the Web logic server?
I want to deploy this Webservice in production. How can I provide security to this?
In another site, I read using Connection, Swagger we can implement it.
I am actually written using flask, flask-jsonpify, flask-sqlalchemy, flask-restful.
Please suggest which packages i need to use to deploy it as WebService.
Let me know in case of any other details needed. Thanks in advance for your suggestions and guidance.
Vijay
The flask itself behaves like a server?
It can
Can I deploy the python Webservice in the Web logic server?
Not unless you are using Jython as WebLogic runs Java applications
I want to deploy this Webservice in production. How can I provide security to this?
See next point
In another site, I read using Connection, Swagger we can implement it.
See next point
I am actually written using flask, flask-jsonpify, flask-sqlalchemy, flask-restful.
Sounds like you've done some research into what packages you need. Maybe find more to get swagger and security figured out
Please suggest which packages i need to use to deploy it as WebService.
Refer point 1. flask is all you need to run the web server

What is considered the current industry standard alternative to CGI for Python web development?

For one of my classes last semester I had to create a website using a CGI script. While it was a good introduction to web development, but I can see it has it's limitations. So my question is, in the industry today, what is the most common approach for parsing a Python script into html?
I have found a few posts on Stack Overflow that suggest FastCGI for PHP, but I am wondering if the same answers apply to Python.
I assume that I would use .py rather than .cgi? If that's the case, how would a web server know to run it?
This is a old way of thinking :)
Nowadays you start your Python process, and it begins to listen on a port. The Python process itself contains a webserver and is almost completely self-contained - see here for the different webservers you can use within your Python process - gunicorn is a popular one.
Of course a lot of people still put Nginx or Apache in front of their Python process as a "reverse proxy". Essentially this is to handle SSL/TLS but also sometimes to do load balancing, custom error pages if your Python process is down etc.
WSGI is a protocol that describes (synchronous) communication between a web server and a web application. https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface

Difference between WSGI utilities and Web Servers [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 9 years ago.
Improve this question
I am new to Python and i am not able to understand the server concepts in Python.
First of all what is WSGI and what are Wsgiref and Werkzeug and how are they different from CherryPy WSGI Server, Gunicorn, Tornado (HTTP Server via wsgi.WSGIContainer), Twisted Web, uWSGI, Waitress WSGI Server.
If i need to develop a web application from scratch, i mean starting from the beginning, where should i start, my company needs a custom framework and the application is based on critical performance overheads.
Please help and explain how they are different.
P.S I am not a beginner to programming.
WSGI is just a set a rules to help unify and standardize how Python applications communicate with web servers. It defines both how applications should send responses and how servers should communicate with applications and pass along the environment and other details about the request. Any application that needs to communicate with any web server implements WSGI, because its the de-facto standard and recommended method for Python. WSGI came about to unify the other implementations (CGI, mod_python, FastCGI).
wsgiref is a reference implementation of the WSGI interface. Its like a blueprint to help developers understand how to implement WSGI in their own applications.
The other things you mentioned are all different applications that implement the WSGI standard; with some exceptions:
Twisted is a library to create applications that can communicate over a network. Any kind of network, and any kind of applications. Its not limited to the web.
Tornado is similar to Twisted in that it is also a library for network communication; however it is designed for non blocking applications. Things that require a long open connection to the server (like say, an application that displays realtime feeds).
CherryPy is a very minimal Python framework for creating web applications. It implements WSGI.
Werkzeug is a library that implements WSGI. So if you are developing an application that needs to speak WSGI, you would import werkzeug because it provides all various parts of WSGI that you would need.
uWSGI is a project that allows easily hosting of multiple web applications. The fact that it as WSGI in the name is because WSGI was the first plugin that was released with the application. It is perhaps the odd duck in this list because it is not a development framework, but more of a way to manage multiple web applications.
Web servers that implement WSGI can talk to any application that also implements WSGI. modwsgi is a popular implementation of WSGI for webservers; it is available for both Apache and Nginx and for IIS there is the isapi wsgi module.

Minimal, Standalone, Distributable, cross platform web server

I've been writing a fair number of smaller wsgi apps lately and am looking to find a web server that can be distributed, preconfigured to run the specific app. I know there are things like twisted and cherrypy which can serve up wsgi apps, but they seem to be missing a key piece of functionality for me, which is the ability to "pseudostream" large files using the http range header. Is there a web server available under a BSD or similar license which can be distributed as a standalone executable on any of the major platforms which is capable of both proxying to a a wsgi server (like cherrypy or the like) AND serving large files using the http range header?
Lighttpd has a BSD license, so you should be able to bundle it if you wanted.
You say its for small apps, so I guess that means, small, local, single user web interfaces being served by a small http server? If thats is the case, then any python implementation should work. Just use something like py2exe to package it (in fact, there was a question relating to packaging python programs here on SO not too long ago).
Update, re: range header:
The default python http server may not support the range header you want, but its pretty easy to write your own handler, or a small wsgi app to do the logic, especially if all you're doing is streaming a file. It wouldn't be too many lines:
def stream_file(environ, start_response):
fp = open(base_dir + environ["PATH_INFO"])
fp.seek(environ["HTTP_CONTENT_RANGE"]) # just an example
start_response("200 OK", (('Content-Type', "file/type")))
return fp
What's wrong with Apache + mod_wsgi? Apache is already multiplatform; it's often already installed (except in Windows).
You might also want to look at lighttpd, there are some blogs on configuring it to work with WSGI. See http://cleverdevil.org/computing/24/python-fastcgi-wsgi-and-lighttpd, and http://redmine.lighttpd.net/issues/show/1523

Cleanest & Fastest server setup for Django [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm about to deploy a mediumsized site powered by Django. I have a dedicated Ubuntu Server.
I'm really confused over which serversoftware to use. So i thought to myself: why not ask stackoverflow.
What i'm looking for is:
Easy to set up
Fast and easy on resources
Can serve mediafiles
Able to serve multiple djangosites on same server
I would rather not install PHP or anything else that sucks resources, and for which I have no use for.
I have heard of mod_wsgi and mod_python on Apache, nginx and lighty. Which are the pros and cons of these and have i missed someone?
#Barry: Somehow i feel like Apache is to bloated for me. What about the alternatives?
#BrianLy: Ok I'll check out mod_wsgi some more. But why do i need Apache if i serve static files with lighty? I have also managed to serve the django app itself with lighty. Is that bad in anyway? Sorry for beeing so stupid :-)
UPDATE: What about lighty and nginx - which are the uses-cases when these are the perfect choice?
Since I was looking for some more in-depth answers, I decided to research the issue myself in depth. Please let me know if I've misunderstood anything.
Some general recommendation are to use a separate webserver for handling media. By separate, I mean a webserver which is not running Django. This server can be for instance:
Lighttpd (Lighty)
Nginx (EngineX)
Or some other light-weight server
Then, for Django, you can go down different paths. You can either:
Serve Django via Apache and:
mod_python
This is the stable and recommended/well documented way. Cons: uses a lot of memory.
mod_wsgi
From what I understand, mod_wsgi is a newer alternative. It appears to be faster and easier on resources.
mod_fastcgi
When using FastCGI you are delegating the serving of Django to another process. Since mod_python includes a python interpreter in every request it uses a lot of memory. This is a way to bypass that problem. Also there is some security concerns.
What you do is that you start your Django FastCGI server in a separate process and then configures apache via rewrites to call this process when needed.
Or you can:
Serve Django without using Apache but with another server that supports FastCGI natively:
(The documentation mentions that you can do this if you don't have any Apache specific needs. I guess the reason must be to save memory.)
Lighttpd
This is the server that runs Youtube. It seems fast and easy to use, however i've seen reports on memoryleaks.
nginx
I've seen benchmarks claiming that this server is even faster than lighttpd. It's mostly documented in Russian though.
Another thing, due to limitations in Python your server should be running in forked mode, not threaded.
So this is my current research, but I want more opinions and experiences.
I'm using Cherokee.
According to their benchmarks (grain of salt with them), it handles load better than both Lighttpd and nginx... But that's not why I use it.
I use it because if you type cherokee-admin, it starts a new server that you can log into (with a one-time password) and configure the whole server through a beautifully-done webmin. That's a killer feature. It has already saved me a lot of time. And it's saving my server a lot of resources!
As for django, I'm running it as a threaded SCGI process. Works well. Cherokee can keep it running too. Again, very nice feature.
The current Ubuntu repo version is very old so I'd advise you use their PPA. Good luck.
As #Barry said, the documentation uses mod_python. I haven't used Ubuntu as a server, but had a good experience with mod_wsgi on Solaris. You can find documentation for mod_wsgi and Django on the mod_wsgi site.
A quick review of your requirements:
Easy to setup I've found apache 2.2 fairly easy to build and install.
Fast and easy on resources I would say that this depends on your usage and traffic. * You may not want to server all files using Apache and use LightTPD (lighty) to server static files.
Can serve media files I assume you mean images, flash files? Apache can do this.
Multiple sites on same server Virtual server hosting on Apache.
Rather not install other extensions Comment out anything you don't want in the Apache config.
The officially recommended way to deploy a django project is to use mod_python with apache. This is described in the documentation. The main pro with this is that it is the best documented, most supported, and most common way to deploy. The con is that it probably isn't the fastest.
The best configuration is not so known I think. But here is:
Use nginx for serving requests (dynamic to app, static content directly).
Use python web server for serving dynamic content.
Two most speedy solutions for python-based web server is:
cogen
fapws2
You need to look into google to find current best configuration for django (still in development).
I’m using nginx (0.6.32 taken from Sid) with mod_wsgi. It works very well, though I can’t say whether it’s better than the alternatives because I never tried any. Nginx has memcached support built in, which can perhaps interoperate with the Django caching middleware (I don’t actually use it, instead I fill the cache manually using python-memcache and invalidate it when changes are made), so cache hits completely bypass Django (my development machine can serve about 3000 requests per second).
A caveat: nginx’ mod_wsgi highly dislikes named locations (it tries to pass them in SCRIPT_NAME), so the obvious ‘error_page 404 = #django’ will cause numerous obscure errors. I had to patch mod_wsgi source to fix that.
I'm struggling to understand all the options as well. In this blog post I found some benefits of mod_wsgi compared to mod_python explained.
Multiple low-traffic sites on a small VPS make RAM consumption the primary concern, and mod_python seems like a bad option there. Using lighttpd and FastCGI, I've managed to get the minimum memory usage of a simple Django site down to 58MiB virtual and 6.5MiB resident (after restarting and serving a single non-RAM-heavy request).
I've noticed that upgrading from Python 2.4 to 2.5 on Debian Etch increased the minimum memory footprint of the Python processes by a few percent. On the other hand, 2.5's better memory management might have a bigger opposite effect on long-running processes.
Keep it simple: Django recommends Apache and mod_wsgi (or mod_python). If serving media files is a very big part of your service, consider Amazon S3 or Rackspace CloudFiles.
There are many ways, approach to do this.For that reason, I recommend to read carefully the article related to the deployment process on DjangoAdvent.com:
Eric Florenzano - Deploying Django with FastCGI: http://djangoadvent.com/1.2/deploying-django-site-using-fastcgi/
Read too:
Mike Malone - Scaling Django
Stochastictechnologies Blog: The perfect Django Setup
Mikkel Hoegh Blog: 35 % Response-time-improvement-switching-uwsgi-nginx
Regards
In my opinion best/fastest stack is varnish-nginx-uwsgi-django.
And I'm successfully using it.
If you're using lighthttpd, you can also use FastCGI for serving Django. I'm not sure how the speed compares to mod_wsgi, but if memory serves correctly, you get a couple of the benefits that you would get with mod_wsgi that you wouldn't get with mod_python. The main one being that you can give each application its own process (which is really helpful for keeping memory of different apps separated as well as for taking advantage of multi-core computers.
Edit: Just to add in regards to your update about nginix, if memory serves correctly again, nginix uses "greenlets" to handle concurrency. This means that you may need to be a little bit more careful to make sure that one app doesn't eat up all the server's time.
We use nginx and FastCGI for all of our Django deployments. This is mostly because we usually deploy over at Slicehost, and don't want to donate all of our memory to Apache. I guess this would be our "use case".
As for the remarks about the documentation being mostly in Russian -- I've found most of the information on the English wiki to be very useful and accurate. This site has sample configurations for Django too, from which you can tweak your own nginx configuration.
I have a warning for using Cherokee. When you make changes to Django Cherokee maintains the OLD process, instead of killing it and starting a new one.
On Apache i strongly recommend this article.
http://www.djangofoo.com/17/django-mod_wsgi-deploy-exampl
Its easy to setup, easy to kill or reset after making changes.
Just type in terminal
sudo /etc/init.d/apache2 restart
and changes are seen instantly.

Categories

Resources