views:

275

answers:

4

Hello everyone, I am trying to learn web programming in python. I am converting my old php-flash project into python. Now, I am confused about how to set param value and create object using python.

FYI I used a single php file, index.php to communicate with flash.swf. So, my other php files like login.php, logout.php, mail.php, xml.php etc used to be called from this.

Below is the flash object call from index.php-

<object classid="clsid:XXXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBB" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="100%" height="100%" id="main" align="middle">
<param name="allowScriptAccess" value="all" />
<param name="flashvars" value= />
<param name="movie" value="flash.swf?<?=substr($_SERVER["REQUEST_URI"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);?>" />

<param name="loop" value="false" />
<param name="quality" value="high" />
<param name="bgcolor" value="#eeeeee" />
<embed src="flash.swf?<?=substr($_SERVER["REQUEST_URI"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);?>" loop="false" quality="high" bgcolor="#eeeeee" width="100%" height="100%" name="main" align="middle" allowScriptAccess="all" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

Can any geek help me out with examples how I can convert this into python? Or, any reference on how it can be done?

Cheers :)

A: 

php itself can be used as templating language for generatin html, but in python you will need to use one of the several templating engines available. If you want you can do simple templating using string.Template class but that is not what you would want to do.

Your first step should be to decide on web framework you are going to use, and use templating it provides, e.g django

But if you want just a plane cgi python script, you will need to write your html to stdout. so create a simple html and replace template values with your parameters e.g.

from string import Template 

htmlTemplate = Template(""" 
<html>
<title>$title</title>
</html> 
""")

myvalues = {'title':'wow it works!'}
print "Content-type: text/html"
print
print htmlTemplate.substitute(myvalues)

to work with cgi you can use cgi module e.g.

import cgi
form = cgi.FieldStorage()
Anurag Uniyal
Well, but I don't get how I will pass the parameters in/out of flash? Like see in my code , I used $_SERVER["REQUEST_URI"], strrpos($_SERVER["SCRIPT_NAME"] .. Now, assuming I have mail.py , login.py, logout.py - how to do that?
Morison
I dont' have exp. with web2py, but it must have some view/function where you will render html template, there from request object you can get all the variables like URI etc and pass to template. I don't know why you need script name, in such frameworks you can have single script
Anurag Uniyal
No, I don't need script names. Those are just for example... I meant function/method calls.
Morison
I think you have single html template, which you want to render from different function, so in each function you can send the different name/functionname to template for rendering
Anurag Uniyal
+3  A: 

Python is a general purpose language, not exactly made for web. There exists some embeddable PHP-like solutions, but in most Python web frameworks, you write Python and HTML (template) code separately.

For example in Django web framework you first write a view (view — you know — from that famous Model-View-Controller pattern) function:

def my_view(request, movie):
    return render_to_template('my_view.html',
                              {'movie': settings.MEDIA_URL + 'flash.swf?' + movie})

And register it with URL dispatcher (in Django, there is a special file, called urls.py):

...
url(r'/flash/(?P<movie>.+)$', 'myapp.views.my_view'),
...

Then a my_view.html template:

...
<object classid="clsid:XXXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBB" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="100%" height="100%" id="main" align="middle">
<param name="allowScriptAccess" value="all" />
<param name="flashvars" value= />
<param name="movie" value="{{ movie }}" />
<param name="loop" value="false" />
<param name="quality" value="high" />
<param name="bgcolor" value="#eeeeee" />
<embed src="{{ movie }}" loop="false" quality="high" bgcolor="#eeeeee" width="100%" height="100%" name="main" align="middle" allowScriptAccess="all" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
...

While this may seem like a lot of work for such a tiny task, when you have to write something bigger than simple value-substituting script, the framework pays back. For example, you may actually write a simple blog application in less than 100 lines of code. The framework will automatically take care of URL parsing (somehow like Apache's mod_rewrite for PHP), complex templating, database access, form generation, processing and validation, user authentication, debugging and so on.

There are a lot of different frameworks, each having its own good and bad sides. I recommend spending some time reading introductions and choosing one you like. Personally, I like Django, and had success with web.py. I've also heard good things about Pylons and TurboGears.

If you need something really simple (like in your example), where you don't need almost anything, you may just write small WSGI application, which then can be used, for example, with Apache's mod_python or mod_wsgi. It will be something like this:

def return_movie_html(environ, start_response):
    request_uri = environ.get('REQUEST_URI')
    movie_uri = request_uri[request_uri.rfind('/')+1:]
    start_response('200 OK', [('Content-Type', 'text/html')])
    return ['''
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                                  "http://www.w3.org/TR/html4/strict.dtd"&gt;
            <html>
            ...
            <object ...>
            <param name="allowScriptAccess" value="all" />
            <param name="flashvars" value= />
            <param name="movie" value="%(movie)s" />
            <param name="loop" value="false" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#eeeeee" />
            <embed src="%(movie)s" loop="false" ... />
            </object>
            ...
            </html>
            ''' % {'movie': movie_uri}]

To sum it up: without additional support libraries, Python web programming is somehow painful, and requires doing everything from the URI parsing to output formatting from scratch. However, there are a lot of good libraries and frameworks, to make job not only painless, but sometimes even pleasant :) Learn about them more, and I believe you won't regret it.

drdaeman
Thanks for your suggestion. I am trying in web2py as it seemed easy for little works to me.
Morison
Sorry, I've tried searching for documentation on web2py, but failed. There is some pattern-matching (?P<param>...) in routes.py, which seems relevant, but I failed to understand how the data is passed to controllers.
drdaeman
"from that famous Model-View-Controller pattern" – you mean, Model-Template-View, right?
Nikhil Chelliah
A: 

You can also try using simple web framework like web.py which has a simple templating system along with a simple ORM for database related functionalities. A basic tutorial is available here which is enough to help you get a simple web page, such as yours, up and running.

Technofreak
A: 

First of all just make a web2py view that contains {{=BEAUTIFY(response.env)}} you will be able to see all environment systems variables defined in web2py.

Look into slide 24 (www.web2py.com) to see the default mapping of urls into web2py variables.

To solve your problem, the easier way would be to change the paths in the flash code, but I will assume you do not want to do that. I will assume instead your urls all look like

http://127.0.0.1:8000/[..script..].php[..anything..]

and your web2py app is called "app".

Here is what you do:

Create routes.py in the main web2py folder that contains

routes_in=(('/(?P<script>\w+)\.php(?P<anything>.*)',
            '/app/default/\g<script>\g<anything>'),
          (('/flash.swf','/app/static/slash.swf'))
routes_out(('/app/default/(?P<script>\w+)\.php(?P<anything>.*)',
            '/\g<script>\.php\g<anything>'),)

this maps

http://127.0.0.1:8000/index.php into http://127.0.0.1:8000/app/default/index
http://127.0.0.1:8000/index.php/junk into http://127.0.0.1:8000/app/default/index/junk
http://127.0.0.1:8000/flash.swf into http://127.0.0.1:8000/app/static/flash.swf

create a controller default.py that contains

def index(): return dict()

Put the file "flash.swf" in the "app/static" folder.

Create a view default/index.html that contains

<object classid="clsid:XXXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBB" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="100%" height="100%" id="main" align="middle">
<param name="allowScriptAccess" value="all" />
<param name="flashvars" value= />
<param name="movie" value="flash.swf?{{=request.env.web2py_original_uri[len(request.function)+5:]}}" />

<param name="loop" value="false" />
<param name="quality" value="high" />
<param name="bgcolor" value="#eeeeee" />
<embed src="flash.swf?{{=request.env.web2py_original_uri[len(request.function)+5:]}}" />
" loop="false" quality="high" bgcolor="#eeeeee" width="100%" height="100%" name="main" align="middle" allowScriptAccess="all" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

I am not sure on whether it is "+5" or "+4"above. Give it a try.

I suggest moving this discussion on the web2py mailing list since there is a much simpler way by changing the paths.

mdipierro
Thanks a lot. I didn't see your answer before. However I learned the URL building technique yesterday.
Morison