TAGS :Viewed: 8 - Published at: a few seconds ago

[ Python CherryPy Web Progamming Structure ]

I usually programmed web application in PHP. For now I am learning using CherryPy framework of Python to do web program. What I am trying to do is to accept a http Get requset from user, then using the variables in the query string to do some function, finally I will return the result. But now I think I am being stuck in the programming convention of CherryPY.

I tried to use the index function to accept the variables post by GET method and then do function as follows:

import cherrypy
from sqlalchemy import *

from function import function   

class WelcomePage:

def index(self,number):

    if not number:
        return "failure";
        return ;    

now = datetime.datetime.utcnow();
code = generateCode();

def finalResult():
    return code;

The above code does not work, it will just end at the return statement of index(self,number) and I cannot continue the other function. I guess I am breaking the object oriented structure of Python style (different from PHP). So is the following is only the proper way to handle the GET input and return after calculation

 def index(self,number):

  if not number:
    return "failure";
    cal = Calculation();
    other =OtherFunction();  
    final =other.doAnotherThing(code);
    return final;   

That is I need to call all other function within index function and then return the result within the index function. Is that the only way to have something to do about the http GET variable (or post). Can I have alternative way to write the code that does not need to call all the function inside the index funcion ( that seems will make the index function so lengthy). Is that a more clean and clear way to finished the processing of the GET variable with different function and then finally return the calculated result? I have tried to google for many days but still can't figure out a good way. Thanks

Answer 1

First, coming from PHP you should understand syntax (e.g. you don't need semi-colons unless it is a multi-statement line) and semantics (e.g. Python OO capabilities to arrange your code, your "programming structure") of Python. Second, workflow difference between CherryPy application and common PHP deployment (you can read PHP is meant to die for instance). CherryPy to your code is threaded application-server. Your active code is always in memory and can run in background. You have as the more power as the more responsibility.

Once you know the basics you can arrange your code the way you like. Compose or decompose, put in functions or classes or separate modules or any other imaginable way that Python can handle. CherryPy here is no limit. In its simplest it could be something as follows.

#!/usr/bin/env python

import cherrypy

config = {
  'global' : {
    'server.socket_host' : '',
    'server.socket_port' : 8080,
    'server.thread_pool' : 4

class App:

  def _foo(self, value):
    return self._bar(value)[::-1]

  def _bar(self, value):
    return value * 2

  def index(self, value = None):
    if not value:
      # when you visit /
      raise cherrypy.HTTPError(500)
      # when you visit e.g. /?value=abc
      return self._foo(value)

if __name__ == '__main__':
  cherrypy.quickstart(App(), config = config)