uWSGI - Serving multiple python files with only uWSGI behind nginx - python

Pardon me, this question is going to be too basic but i have not been able to get the solution anywhere. So, here goes ..
I have set up an instance nginx on Ubuntu 14.04 and an instance of uWSGI on port 9090.
I can direct the traffic from nginx to that one instance of uWSGI, and it works.
But every hello world example i see has something like this
> def application(env, start_response):
> start_response('200 OK', [('Content-Type','text/html')])
> return "Hello World From uWSGI"
and it is just one file... so, if i have multiple python files that needs to be served, distributed through a tree of subdirectories [basically my project]. How can I set that directory structure available for use through HTTP requests?
I would NOT like to use any frame work like Django of flask. But serve simple python file, similar to how a pure html site distributed through subdirectories would serve html.
i might also be missing some concept of how routing can be done. As i feel that has never come up till now.
The directory structure has logic that manipulates a database or interacts with thrid party apis.
Need some guidance to start off.
[Please let me know if there is totally a different way to archive what i am in need of, or if i am on the right track]
Thanks.

The application function is responsible for everything. Of you want to do routing, you do it there, by parsing the request path in env['PATH_INFO'] and then importing and calling the relevant Python functions.
But that would be rather a silly thing to do, seeing is that is exactly what Flask would do for you. You don't need to use anything else from the framework it you really don't want to.

Related

Web application with python

I have a Python program that takes one input (string) and prints out several lines as an output. I was hoping to get it online, so that the web page only has a text-area and a button. Inserting a string and pressing the button should send the string to python and make the original page print out the output from Python.
Assuming I have only dealt with Python and a little bit of HTML and have very limited knowledge about the web, how might I approach this problem?
For some reason, my first instinct was PHP, but Googling "PHP Python application" didn't really help.
That is, assuming what I want IS a 'web application', right? Not a web server, web page or something else?
How could I get a really simple version (like in the first paragraph) up and running, and where to learn more?
Yes, the term you're looking for is "web application". Specifically, you want to host a Python web application.
Exactly what this means, can vary. There are two main types of application - cgi and wsgi (or stand-alone, which is what most people talk about when they say "web app").
Almost nobody uses cgi nowadays, you're much more likely to come across a site using a web application framework, like Django or Flask.
If you're worried about easily making your page accessible to other people, a good option would be to use something like the free version of Heroku do to something simple like this. They have a guide available here:
https://devcenter.heroku.com/articles/getting-started-with-python#introduction
If you're more technically inclined and you're aware of the dangers of opening up your own machine to the world, you could make a simple Flask application that does this and just run it locally:
from flask import Flask, request
app = Flask(__name__)
#app.route('/')
def main():
return 'You could import render_template at the top, and use that here instead'
#app.route('/do_stuff', methods=['POST'])
def do_stuff():
return 'You posted' + request.form.get('your string')
app.run('127.0.0.1', port=5555, debug=True)
Pick a Python web development framework and follow the getting started tutorial. Main options are Django for a "batteries included" approach or Flask for a minimalistic bottom-up approach.

Google App Engine folder url's in Python

In google App Engine, If I already have a page setup so everything gets redirected to main.py as shown below:
www.foobar.appspot.com/
will always go to main.py
the main.py is setup pretty much the same way as the hello world tutorial (except it calls some more functions that I wrote but that is irrelevant).
Now my question is, how can I set up multiple pages in the format of "Folders" or do anything with urls really... (I have not had much luck with reading through the documentation)
www.foobar.appspot.com/admin/
would go to a class or something, that would obviously handle all of the admin features (for now I would love a hello world). I really do not no, what script I would have that url go to, I am pretty clueless on urls with google App engine. I have seen posts where people had multiple classes in the main.py script but I could not figure out how to change the yaml file to accomidate that.
Your main.py will need to look at the URL and route it to whatever handler function you use.
This is a common task and most web frameworks have some way to handle it. For example, if you're using webapp2:
http://webapp-improved.appspot.com/guide/routing.html
URLs are mapped to python scripts in app.yaml; the first regex you have in there that matches /admin will determine what's executed.
In the Python files themselves, you also need to handle the URLs that are dispatched to them (how you do this depends on what framework you're using; consult your documentation for how to do routing.)

How are web apps written in Python deployed?

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

Simple and effective web framework

I'm looking for a suitable cross-platform web framework (if that's the proper term). I need something that doesn't rely on knowing the server's address or the absolute path to the files. Ideally it would come with a (development) server and be widely supported.
I've already tried PHP, Django and web2py. Django had an admin panel, required too much information (like server's address or ip) and felt unpleasant to work with; PHP had chown and chmod conflicts with the server (the code couldn't access uploaded files or vice versa) and couldn't handle urls properly; web2py crashed upon compiling and the manual didn't cover that -- not to mention it required using the admin panel. Python is probably the way to go, but even the amount of different web frameworks and distributions for Python is too much for me to install and test individually.
What I need is a simple and effective cross-platform web development language that works pretty much anywhere. No useless admin panels, no fancy user interfaces, no databases (necessarily), no restrictions like users/access/levels and certainly no "Web 2.0" crap (for I hate that retronym). Just an all-powerful file and request parser.
I'm used to programming in C and other low level languages, so difficulty is not a problem.
This question is based on a complete failure to understand any of the tools you have apparently "investigated", or indeed web serving generally.
Django has an admin panel? Well, don't use it if you don't want to. There's no configuration that needs to be done there, it's for managing your data if you want.
PHP has chown problems? PHP is a language, not a framework. If you try and run something with it, you'll need to set permissions appropriately. This would be the case whatever language you use.
You want something that doesn't need to know its address or where its files are? What does that even mean? If you are setting up a webserver, it needs to know what address to respond to. Then it needs to know what code to run in response to a request. Without configuring somewhere the address and the path to the files, nothing can ever happen.
In web2py you do not need to use the admin interface. It is optional.
Here is how you create a simple app from zero:
wget http://web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip
cd web2py/applications
mkdir myapp
cp -r ../welcome/* ./
Optional Edit your app
emacs controllers/default.py
emacs models/db.py
emacs views/default/index.html
...
(you can delete everything in there you do not need).
Now run web2py and try it out
cd ../..
python web2py.py -i 127.0.0.1 -p 8000 -a chooseapassword &
wget http://127.0.0.1:8000/myapp/default/index.html
When you edit controller/default.py you have a controller for example
def index():
the_input = request.vars # this is parsed from URL
return dict(a=3,b=5,c="hello")
You can return a dict (will be parsed by the view with same name as action) or a string (the actual page content). For example:
def index():
name = request.vars.name or 'anonymous'
return "hello "+name
and call
wget http://127.0.0.1:8000/myapp/default/index?name=Max
returns
'hello Max'
/myapp/default/index?name=Max calls the function index, of the controller default.py of the application in folder applications/myapp/ and passes name=Max into request.vars.name='Max'.
I think you need to be more specific about what you want to achieve, and what kind of product(s) you want to develop. A "no setup required" product may come with tons of auto-configuration bloat, while a framework requiring a small setup file could be set up in minutes, too, with much more simplicity in the long run. There is also always going to be some security and access rights to be taken into consideration, simply because the web is an open place.
Also, a framework supporting Web 2.0ish things doesn't have to be automatically a bad framework. Don't throw away good options because they also do things you don't like or need, as long as they allow you to work without them.
PHP had chown and chmod conflicts with the server (the code couldn't access uploaded files or vice versa) and couldn't handle urls properly;
PHP is not a framework in itself, it's a programming language. I don't know which PHP-based framework or product you tried but all the problems you describe are solvable, and not unique to PHP. If you like the language, maybe give it another shot. Related SO questions:
What PHP framework would you choose for a new application and why?
What’s your ‘no framework’ PHP framework?
If you need something that runs everywhere (i.e. on as many servers as possible) PHP will naturally have to be your first choice, simply because it beats every other platform in terms of cheap hosting availability.
If I were you, I wouldn't limit my options that much at this point, though. I hear a lot of good things about Django, for example. Also, the Google App engine is an interesting, scalable platform to do web work with, supporting a number of languages.
Werkzeug:
import werkzeug
#werkzeug.Request.application
def app(request):
return werkzeug.Response("Hello, World!")
werkzeug.run_simple("0.0.0.0", 4000, app)
You can optionally use werkzeug URL routing (or your own, or anything from any other framework). You can use any ORM or template engine for Python you want (including those from other Python frameworks) etc.
Basically it's just Request and Response objects built around WSGI plus some utilities. There are more similar libraries available in Python (for example webob or CherryPy).
What I need is a simple and effective cross-platform web development language that works pretty much anywhere.
Have you tried HTML?
But seriously, I think Pekka is right when he says you need to specify and clarify what you want.
Most of the features you don't want are standard modules of a web app (user and role mgmt., data binding, persistence, interfaces).
We use any or a mix of the following depending on customer requirements: perl, PHP, Flash, Moonlight, JSP, JavaScript, Java, (D/X)HTML, zk.
I am python newbie but experienced PHP developer for 12 years but I have to admit that I migrated to python because of the bottle framework. I am African so you don't have to be extra smart to use it... Give it a try, you'll love it. Hey and it also runs on appspot with no config!
Install python
Download bottle.py (single file)
Create
#your file name : index.py
from bottle import route, run
#route('/')
def index():
return 'jambo kenya! hakuna matata na bottle. hehehe'
run()
Sit back, sip cocoa and smile :)
I'd say Ruby on Rails is what you're looking for. Works anywhere, and no configuration needed. You only have it installed, install the gems you need, and off you go.
I also use ColdFusion, which is totally multi-platform, but relies on the Administrator settings for DSN configuration and stuff.
TurboGears: Everything optional.
Give bottle a try. I use it for my simple no-frills webapps. It is very intuitive and easy to work with in my experience.
Here is some sample code, and it requires just bottle.py, no other dependencies.
from bottle import route, run
#route('/')
def index():
return 'Hello World!'
run(host='localhost', port=8080)
Just stumbled upon Quixote recently. Never used it though.
Use plain old ASP. IIS does not care where files are stored. All paths can be set relative from the virtual directory. That means you can include "/myproject/myfile.asp", whereas in PHP it's often done using relative paths. Global.asa then contains global configuration for the application. You hardly ever have to worry about relative paths in the code.
In PHP you'd have include(dirname(FILE) . '/../../myfile.php") which is of course fugly. The only 'solution' I found for this is making HTML files and then using SSI (server side includes).
The only downside to ASP is the availability, since it has to run on Windows. But ASP files just run, and there's not complex Linux configuration to worry about. The language VBScript is extremely simple, but you can also choose to write server side JavaScript, since you're familiar with C.
I think you need to focus on Restful web applications. Zend is a PHP based MVC framework.

Python web framework with low barrier to entry

I am looking for a LAMPish/WAMPish experience.
Something very transparent. Write the script, hit F5 and see the results. Very little, if any abstraction.
SQLAlchemy and (maybe) some simple templating engine will be used.
I need simple access to the environment - similar to the PHP way. Something like the COOKIE, SESSION, POST, GET objects.
I don't want to write a middleware layer just to get some web serving up and running. And I do not want to deal with specifics of CGI.
This is not meant for a very complex project and it is for beginning programmers and/or beginning Python programmers.
An MVC framework is not out of the question. ASP.NET MVC is nicely done IMO. One thing I liked is that POSTed data is automatically cast to data model objects if so desired.
Can you help me out here?
Thanks!
PS: I did not really find anything matching these criteria in older posts.
CherryPy might be what you need. It transparently maps URLs onto Python functions, and handles all the cookie and session stuff (and of course the POST / GET parameters for you).
It's not a full-stack solution like Django or Rails. On the other hand, that means that it doesn't lump you with a template engine or ORM you don't like; you're free to use whatever you like.
It includes a WSGI compliant web server, so you don't even need Apache.
For low barrier to entry, web.py is very very light and simple.
Features:
easy (dev) deploy... copy web.py folder into your app directory, then start the server
regex-based url mapping
very simple class mappings
built-in server (most frameworks have this of course)
very thin (as measured by lines of code, at least) layer over python application code.
Here is its hello world:
import web
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
if not name:
name = 'world'
return 'Hello, ' + name + '!'
if __name__ == "__main__":
app.run()
As much as I like Werkzeug conceptually, writing wsgi plumbing in the Hello, World! is deeply unpleasant, and totally gets in the way of actually demoing an app.
That said, web.py isn't perfect, and for big jobs, it's probably not the right tool, since:
routes style systems are (imho) better than pure regex ones
integrating web.py with other middlewares might be adventurous
What you're describing most resembles Pylons, it seems to me. However, the number of web frameworks in/for Python is huge -- see this page for an attempt to list and VERY briefly characterize each and every one of them!-)
Have you looked into the Django web framework? Its an MVC framework written in python, and is relatively simple to set up and get started. You can run it with nothing but python, as it can use SQLite and its own development server, or you can set it up to use MySQL and Apache if you'd like.
Pylons is another framework that supports SQLAlchemy for models. I've never used it but it seems promising.
Look at:
WSGI, the standard Python API for HTTP servers to call Python code.
Django, a popular, feature-rich, well documented Python web framework
web.py, a minimal Python web framework
Don't forget Bottle. It is a single-file micro web framework with no dependencies and very easy to use. Here is an "Hello world" example:
from bottle import route, run
#route('/')
def index():
return 'Hello World!'
run(host='localhost', port=8080)
And here an example for accessing POST variables (cookies and GET vars are similar)
from bottle import route, request
#route('/submit', method='POST')
def submit():
name = request.POST.get('name', 'World')
return 'Hello %s!' % name
Check out web2py. It runs out of the box with no configuration - even from a USB stick. The template language is pure Python and you can develop your entire app through the browser editor (although I find vim faster ;)

Categories

Resources