Forums

Scratching my head to get started

Hi,

I am fairly new to Python (learning 3.5) but not to web programming as my background is full-stack LAMP (P=PHP) dev.

I have written a non CGI / WSGI python script that I want to convert to a CGI / WSGI script. At the most I probably want to wrap some printed output in HTML (my app return a HTML response code e.g. 200, 404 etc.) [by default my script doesn't need to output anything. It's a fairly simple app, non dynamic etc. Just one URL.]

What's the simplest way to go about this. I've tried manually setting up WSGI and using web2py but I'm really scratching my head.

Thanks in advance, Matthew

OK - so I am answering my own question in case it helps anybody else trying to get started.

I ended up settling on a Flask app. It is relatively straight-forward to get up and running. http://flask.pocoo.org/docs/0.11/quickstart/#public-server

Add a web app and choose Flask.

Edit the WSGI configuration for the app so it looks like this:-

import sys

# add your project directory to the sys.path
project_home = u'/home/YOURUSERNAME/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from hello import app as application

Make sure source code folder is set to: /home/YOURUSERNAME/mysite Then in your python app call it hello.py and import the flask module:-

from flask import Flask

....

Then wrap the whole of your 'main' program in the following code which tells flask to run if the app route is "/" i.e. http://yoursite.pythonanywhere.com/ - I am assuming you could add more routes later for different pages and parts of your app, especially if you are planning to make it RESTFUL. I haven't got that far yet! I hope this helps. M

@app.route('/')
def hello_world():
    REST OF YOUR CODE HERE
    ....
return 0