Python Forum
trying to figure out WSGI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to figure out WSGI
#1
i am reading about WSGI.  but so much documentation seems to be missing the target on explaining what is going on.  one of things i read is that a web app can be be written in python using WSGI and be a standalone thing.  i have been assuming that means it accept incoming web request connections.  so how does one run one of these when they already have a web server running, or how do they run 2 such apps, if the server host has only 1 IP address?  oh i know .... different ports.   how do they do it on port 80 so the URL does not need the port number coded in it?  so i read the mod_wsgi for apache can do this.  but what is it doing?

can someone explain what is going on with wsgi without the sales talk (e.g without "it solves all your problems" or "it makes web apps great again").
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
wsgi is just an interface that describes some conventions for how servers can talk to each other. The wsgi website you make is rarely actually accessible to real people (unless you're developing). So a server designed for public consumption watches port 80 (which is normally apache, nginx, lighttpd). When a request comes in, that edge server will forward the request to your wsgi app. If your app isn't running, it'll be started.

If you want to compare with php, the difference being that when a request comes in for a php page, the php interpreter spins up and parses the file before handling each request. With wsgi, your app only gets parsed a single time, and keeps running in memory (which is why it's based on callbacks, instead of just handling a request).

Does that... answer your question? I don't think it's too complicated, but a lot of the docs make it sound complicated since most wsgi frameworks come packaged with a small server for development, so sometimes your app IS listening to a port, even if that's not how you'd use it in production. So a lot of docs say things like "this is how it works, unless you're in production in which case that's not how it works!"
Reply
#3
i never did understand php in confidence.   my impression is that the interpreter was running in the same process as apache, but i never sought to know exactly how it was done.  i know more about cgi (and, thus, know how bad it is).  i have written my own little web server in python (and in the past, also in C), so i am curious how much of wsgi is implemented in python.

actually i wrote 2 web servers in C.  one was made as an example of a server library implementation that gave an abstract API to the C programmer to create server processes of many types.  the other was a module loader which selected a .so file based on the URL path of the request and called dlopen() and dlsym() anf fork().  it was able to handle more than 5 times the request rate of apache cgi (which was doing the very expensive execve() call).  my python web server was based on the same idea (but not as fast).

what i really want to know about wsgi is probably what the interface is doing.  i'll go check on the PEPs at this point and see if it heps.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
django docs gives a lot of good info, though still not quite enough but maybe enough to discover the rest by testing.

https://docs.djangoproject.com/en/1.11/topics/install/

then scroll down to "Install Apache and mod_wsgi" and read the next two paragraphs.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Well, I think the first source should be PEP 3333
A short explanation but there are some shortcuts: https://www.fullstackpython.com/wsgi-servers.html
It seems like all Python web frameworks like Django, Flask and similar are on top of the wcgi. Or maybe I am wrong.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Apr-28-2017, 11:11 AM)wavic Wrote: It seems like all Python web frameworks like Django, Flask and similar are on top of the wcgi. Or maybe I am wrong.
You are right.

For WSGI is Werkzeug maybe the best overview.
Armin Ronacher did write Werkzeug before writing Flask.
Werkzeug is the underlying structure of Flask.

Have to think of WSGI if worth learning at a lower level.
This is only needed if want to write eg a new web-framework(Python has enough) Hand

WSGI Servers
Quote:WSGI is by design a simple standard interface for running Python code on web.
As a web developer you won't need to know much more than.
Reply
#7
It's just a set of conventions (like most PIPs). A wsgi application is just a python script. The server calls the app and keeps it running, then uses the conventions to "know" which function to call on that running script and how to format the request headers so your script can understand it.

...as opposed to something like Twisted, which cuts a lot of those middle pieces out and consumes the port itself.
Reply
#8
so a wsgi app is run inside the web server process (because calling it is inside the process)?

i just tried to install django.  the ubuntu packagings seem to be inconsistent between 16.04 LTS (my laptop) and 14.04 LTS (my vps server). the latter does not have python3 versions.  so i suspect the latter is a much older version.  so i went for a pip install.   the instructions seem to be bad.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(Apr-29-2017, 03:17 AM)Skaperen Wrote: so a wsgi app is run inside the web server process (because calling it is inside the process)?
It's explained here.

Quote:the ubuntu packagings seem to be inconsistent between 16.04 LTS (my laptop) and 14.04 LTS (my vps server). the latter does not have python3 versions.  so i suspect the latter is a much older version.
Try to always use virtual environment when working with web.
Then what versions are other places dos not matter at all.

Now do i use most Flask.
Can run a demo install Django on Mint 18.1,using Python 3.5.

[Image: OwDjwP.jpg]
Reply
#10
As I got it, the wcgi is between the web server and the python app: app.py <--> wcgi <--> nginx
It's all I need to know for now. Blush
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mod-wsgi in apache Skaperen 4 4,421 Jul-10-2017, 01:31 AM
Last Post: Skaperen
  mod-wsgi Skaperen 2 3,831 Jun-06-2017, 05:12 AM
Last Post: Skaperen
  WSGI vs CGI Skaperen 9 12,191 Jun-01-2017, 05:39 AM
Last Post: Skaperen
  WSGI working examples Skaperen 1 3,489 May-29-2017, 10:45 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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