Deron mentioned many important things so will just try and comment on
the others.

On 9 June 2010 15:13, 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.

You can use lists as Deron mentioned, but StringIO may make it easier to port.

  import StringIO

  def application(environ, start_response):

    output = StringIO.StringIO()

    print >> output, """<html>
        <head>
            <title>Test</title>
        </head>
        <body>
        <h2><center>Hello World</center></h2>"""


   print >> output, 'Hello %s. Welcome to my world!' % name
   print >> output, br*2
   print >> output, 'This can be a fine alternative...'

   print >> output, '</body></html>'

   data = output.getvalue()

   start_response('200 OK', [('Content-Type', 'text/html'),
('Content-Length', str(len(data))])

   return [ data ]

In other words, existing 'print' statements are still used except that
redirect them into 'output', ie., the StringIO instance.

> 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.
>
> Just a few more questions:
>
> 1. How do I access multiple *.wsgi files in a directory using
> WSGIScriptAlias? When I type in localhost/wsgi/ (the path I assigned
> it to) I want to access another file other than the primary one. For
> example 'localhost/wsgi/app2.wsgi'.

Use:

  WSGIScriptAlias /wsgi/ /usr/local/wsgi/scripts/

See:

  http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines

There are also other ways using Alias if you need to interleave WSGI
files with other files in same directory, ie., .php files or static
files.

> 2. Mod_wsgi is compatible with MySQL right? If so, can it handle user
> sessions? (log in with username and password)
> 3. Is there anyway to manually asses how much of an improvement my
> wsgi script is over CGI?

It will be way faster, you don't need to benchmark to work that out.
This is because for CGI a new process is created on each request where
as with persistent WSGI there isn't. See:

  http://code.google.com/p/modwsgi/wiki/PerformanceEstimates

Graham

> Once again thanks for the help. I'll look into the 'Learn WSGI' web
> page later tonight.
>
> On Jun 9, 1:49 pm, Graham Dumpleton <[email protected]>
> wrote:
>> On 9 June 2010 13:38, Shady <[email protected]> wrote:
>>
>>
>>
>> > I've started Python programming a several months ago and have since
>> > began a website completely driven by Python (to enhance my Python
>> > knowledge). My Python web pages were basically standards Python
>> > scripts you can write up in Notepad, but with the added HTML
>> > formatting.
>>
>> > For example, a script of mine looks like this on my PC:
>>
>> > length = range(1,20)
>> > for num in length:
>> >   print num*2
>>
>> > But then I obviously adapt it to output correctly in a browser:
>>
>> > print "Content-Type..."
>> > print
>>
>> > print "<html>....<body>"
>> > length = range(1,20)
>>
>> > for num in length:
>> >   print num*2,"<br />"
>>
>> > print "</body></html>"
>>
>> > I then place it in the cgi-bin and execute it. Works wonderfully. The
>> > thing is, Bluehost's speed has begun to degrade and their Python
>> > support is average at best. So I'm moving to a host with better
>> > support and with better alternatives to CGI. Now, I'm confused with
>> > how I'm going to get my Python pages working under mod_wsgi. Do I have
>> > to completely rewrite each script for it to work?
>>
>> To a degree. WSGI is not CGI.
>>
>> > Is it possible for
>> > my Python scripts in their current format to work under mod_wsgi?
>>
>> Only if you wrote a CGI emulation layer on top and ensure
>> multithreading was never used. It is not worth the trouble doing this.
>> Better to change to using WSGI interface.
>>
>>
>>
>> > I'm
>> > completely lost on how I'm going to create seperate pages too. I've
>> > Googled but found haven't found too much.
>>
>> > I've gotten mod_wsgi to work on my PC and I've gotten this wsgi script
>> > to run (a modified hello world script):
>>
>> > #!C:\Python26\python.exe
>>
>> > def application(environ, start_response):
>> >    numbers = ""
>> >    for x in range(1,20):
>> >        numbers += str(x)+" - "
>> >    file = open("\header.html").read()
>>
>> >    start_response('200 OK', [('Content-Type', 'text/html')])
>> >    return [ file, numbers ]
>>
>> > print "Why won't you work?!"
>>
>> > The thing is, it doesn't print the last line. I know I'm doing
>> > something wrong and I know my idea of what mod_wsgi is probably
>> > skewed. If anyone can shed some light on my issues I'd really
>> > appreciate it. I just want the added bonus of mod_wsgi while using the
>> > standard Python scripting format, but it doesn't seem as if that's
>> > possible...
>>
>> That last line will only print out once when script initially loaded
>> and it would only print to Apache error logs and not be a part of the
>> response returned to the client.
>>
>> I would suggest you read:
>>
>>  http://www.wsgi.org/wsgi/Learn_WSGI
>>
>> Also suggest that you have a look at one of the micro frameworks such
>> as flask. This will make your life a lot easier as provides a lot of
>> high level abstractions for you so you don't have to do as much work.
>> Read:
>>
>>  http://flask.pocoo.org/
>>
>> Flask is one of many WSGI capable web frameworks for writing Python
>> web applications.
>>
>> Graham
>
> --
> 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.
>
>

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