I'm an ex-PHP developer and I'm struggling to get over the mindset that I can write an nginx config script, create the directories, fire up nginx + PHP-FPM, upload my code and let it run. I want to start playing with Python to get a feel for it and start creating web applications, but I'm a little lost as to how to get it all working.

Although some people's advice is invariably going to be to use a framework like Django, I want to see how this all works from the bottom-up.

I've come across a comparison of Python web servers, and it appears that gevent is what I'm looking for. But how does it all fit together? Do I still need nginx (except for static content) as a reverse proxy, or do I do something different? I'm facing "beginner blindness" and I'm struggling to see how it all fits together.

有帮助吗?

解决方案

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...

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top