On Wed, Jun 9, 2010 at 1:13 AM, Shady <[email protected]> wrote:
> Thanks for the quick reply. I threw something together and just wanted
> to know if this is the right approach:
>
> output = ""
> def application(environ, start_response):
>
>    def echo(input):
>        global output
>        output += str(input)+"\n"
>
>    name = "Shady"
>    br = "<br/>"
>
>    echo("""<html>
>        <head>
>            <title>Test</title>
>        </head>
>    <body>
>    <h2><center>Hello World</center></h2>""")
>
>    echo('Hello %s. Welcome to my world!' % name)
>    echo(br*2)
>    echo('This can be a fine alternative...')
>
>    echo('</body></html>')
>    start_response('200 OK', [('Content-Type', 'text/html')])
>
>    return [ output ]
>
> Would using such a function to replace the print command be a feasible
> workaround? That way it'd be much easier to port my code.

Be careful using global variables like your 'output' as it
is not going to be thread safe.   An advantage of using
WSGI is that you are generally insulating your application code
from whatever WSGI-compliant environment you're running
in ... and some of those may use threads (mod_wsgi
among them in certain configurations).

Also since strings are immutable the append operator (+) is
not very efficient if repeated often.  Better would be to just
accumulate all your little strings into an array and then use
the string join method once at the end, such as:

  parts = []
  parts.append( "Hello" )
  parts.append( "World!" )
  ...
  output = "".join( parts )

That's much more efficient and is a general python pattern ... for
wsgi in particular there are other ways to make further optimizations.

If you really want something that acts similar to printing to
a file, you always have the standard StringIO module too.


> I've looked at other frameworks such as Django, but it looks too
> intimidating. Right now I just want to refine my Python skills, but if
> Flask is simple and easy to use, I'll look into it.

Most of the bigger frameworks are designed to do a whole lot
of complex web-based applications.  If all you're doing is basically
outputting static web pages, you can get by with just one of
the micro-frameworks.  Or no framework at all.  See the Learning
WSGI reference that Graham already mentioned.

Also, if you're outputting truly static pages (they are the same
every time they are called), you may want to look at some of
the simple templating frameworks (like Mako templates, Kid, etc.)
as they can automatically handle caching for you.


> 2. Mod_wsgi is compatible with MySQL right? If so, can it handle user
> sessions? (log in with username and password)

mod_wsgi will not conflict with MySQL (although see the compilation
notes if you're not using a already-compiled distribution package).

However mod_wsgi and MySQL are also completely separate things.
It is entirely up to your application, or web framework, to manage your
MySQL connections, transactions, and so forth.

-- 
Deron Meranda
http://deron.meranda.us/

-- 
You received this message because you are subscribed to the Google Groups 
"modwsgi" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/modwsgi?hl=en.

Reply via email to