Domanda

Sono uno sviluppatore di ex-PHP e sto lottando per superare la mentalità che posso scrivere uno script di configurazione nginx, creare le directory, fuoco fino nginx + PHP-FPM, caricare il mio codice e farlo funzionare. Voglio iniziare a giocare con Python per ottenere una sensazione di esso e iniziare a creare applicazioni web, ma io sono un po 'perso su come ottenere tutto di lavoro.

Anche se consiglio di alcune persone è invariabilmente sta per essere quella di utilizzare un framework come Django, voglio vedere come funziona tutto questo dal basso verso l'alto.

Sono venuto attraverso un confronto di server web Python, e sembra che gevent è quello che sto cercando. Ma come fa tutto in forma insieme? Ho ancora bisogno di nginx (tranne che per i contenuti statici) come proxy inverso, o devo fare qualcosa di diverso? Sono di fronte a "principiante cecità" e sto lottando per vedere come si integra il tutto.

È stato utile?

Soluzione

I am a beginner Python developer myself, so I've had the same questions. Perhaps a more advanced user can fill in the details. Here's what I've done so far:

The simplest way to get started is to simply make an executable python script (.py) and drop it in your cgi-bin. You can then access it via yourhost.com/cgi-bin/your_script.py. Simple to do, easy to use for form processing and stuff.

Some servers will require you to restart the server before it can 'see' the new .py script, which could be quite annoying for rapid development. This is one reason why a lot of people use middleware such as WSGI. Here's how I modified my Apache config to enable WSGI:

LoadModule wsgi_module libexec/apache2/mod_wsgi.so

<VirtualHost *:80>

WSGIScriptAlias /myapp /Library/WebServer/wsgi-scripts/views.wsgi

<Directory /Library/WebServer/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

views.wsgi is simply a Python script. Now if I go to localhost/myapp/anything it will redirect to views.wsgi. It's a good idea to not put this file in your root directory, otherwise you will not be able to reference static files.

A simple app might simply look like this:

def application(environ, start_response):
    start_response('200 OK', [('content-type', 'text/html')])
    return ['Hello world!']

environ contains information about the path that the user is trying to reach, so the idea is that you can set up a list of URLS, and tell your program which function to call based on which URL was requested. Something like this:

 path      = environ.get('PATH_INFO','')
 if path.startswith('/helloworld'):
  # call your function that returns HTML code

I haven't dealt much with frameworks (such as Django) yet, but I think one of the advantages there is that they make it easy to fill out HTML templates using whatever variables are passed from your script. Django's template engine allows including variables as well as logic (if, for, etc) intermixed with your HTML. When the function is called, whatever it returns is sent to the client.

I'm still pretty new to all of this, so please correct me if there are any errors here...

Altri suggerimenti

It is little bit different with Python that with PHP. A good thing about Python, that a common interface is defined in a standard that lets one to use various Python applications on one side and different web servers on the other. It is called WSGI, and you can read more about it here

And here is a good post about deploying Django application via ngnix.

You need some software that will execute your pyton code..

You can try a 100% python stack with tornado

Or you can use Apache, Nginx or Lighttpd (i think all written in C) and then use some mod (mod_wsgi, fgci or uWsgi) in order to make this webservers run your code using wsgi interface. This second option is like what apache+some_mod do to run your PHP code..

I have production sites running on Apache+mod_wsgi and Nginx+uWsgi

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top