April 25, 2011

3

Python Server Pages Example of GET POST Form Input

Previously, I've talked about mod_python.publisher method of creating a simple website using Python. And this time, I will talk about mod_python.psp where PSP stands for Python Server Pages.

What good about Python Server Pages for former PHP or JSP programmer is the way you can write your code where you can use the <% # your code here %> tag as you always do. But before that, you have to configure your server to enable mod_python.psp.

Pre-requisite
In order to run your site with PSP, you have to install and enable mod_python. Here is the steps if you are using Ubuntu:
  1. Install apache2 and libapache2-mod-python and enable mod-python
    $ sudo apt-get install apache2
    $ sudo apt-get install libapache2-mod-python
    $ sudo a2enmod python
  2. Edit /etc/apache2/sites-enabled/000-default. Search for this lines:
    <Directory /var/www/>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride None
                    Order allow,deny
                    allow from all
            </Directory>
    
    and add python handler like this:
    <Directory /var/www/>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride None
                    Order allow,deny
                    allow from all
                    
                    AddHandler mod_python .psp
                    PythonHandler mod_python.psp
                    PythonDebug On
                    
            </Directory>
    
  3. Restart your apache
    $ sudo /etc/init.d/apache2 restart
  4. You can also enable index.php for your DirectoryIndex by editing /etc/apache2/mods-enabled/dir.conf and add this string (in red):
    <IfModule mod_dir.c>
              DirectoryIndex index.psp index.html index.cgi index.pl index.php index.xhtml index.htm 
    </IfModule>
    

Sample Python Server Pages Website
And here is my sample code:
<%
import cgi

thetitle "Coder's Talk Python Form Example"

# it doesn't matter whether it is post or get method
# getfirst will find the item.
username form.getfirst('username')
themail form.getfirst('usermail')

%>
<html>
<head>
<title><%= thetitle %>
</title>
</head>
<style>
body {font-familyArialfont-size:13px;}
#contentbox {width: 640px; margin:0 auto;}
.copy, .copy a {width640pxmargin:autocolor#DD8888;}
.formresult {background-color:#FFFF99;display:block;width:100%;padding:10px;}
</style>
<body>
<div id="contentbox"><h1><%= thetitle%></h1>
<%
if username:
    username cgi.escape(username)
%>
<class="formresult">Hello <%= username %>!<br/>
<%
    if themail:
        themail cgi.escape(themail)
%>
Your email is <%= themail %></p>
<%
    else:
%>
You didn't enter your email. You don't have email?</p>
<%
    # end if themail
# end if username
%>
<p>This is my example form page processing using POST method</p>
<form name="myform" method="post" action="index.psp">
Name: <input type="text" name="username"/><br/>
Email: <input type="text" name="usermail"/><br/>
<input type="submit" value="Submit"/>
</form>
<br/>

<p>This is the same thing using GET method.</p>
<form name="myform" method="get" action="index.psp">
Name: <input type="text" name="username"/><br/>
Email: <input type="text" name="usermail"/><br/>
<input type="submit" value="Submit"/>
</form>
</div>
<div class="copy">
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2FCoders-Talk%2F138805102822113&amp;layout=standard&amp;show_faces=false&amp;width=640&amp;action=like&amp;font=tahoma&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:640px; height:35px;" allowTransparency="true"></iframe>
<br/>&copyMFauzilkamil Zainuddin 
| <a href="http://coderstalk.blogspot.com">Coder's Talk</a></div>
</body>
</html>
<%
# end of code. Copyright (C) M. Fauzilkamil Zainuddin (ApOgEE) - http://coderstalk.blogspot.com
%>

You can also download the source code here . Make sure you rename it as index.psp or change the action name on the form tag to be the same as your psp file name. Feel free to try it, modified it and don't forget to 'Like' this blog on Facebook too

Share This Article:


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy




April 24, 2011

2

Python Code to Get IP Address From Interface Name


From my previous code on How To Print Active Network Interface List in Linux using Python, I got this question.

Question from jimchris:
Nice work, now how do we get ip address from ifacedata?

My Answer:
Here's how... Add this function to your python code:
def getifip(ifn):
    import socketfcntlstruct
    sck socket.socket(socket.AF_INETsocket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(sck.fileno(),0x8915,struct.pack('256s'ifn[:15]))[20:24])

Then use it in your previous code like this:
print getifip(ifacedata[0])

Update: you can also download the python source code here

Wish you luck and Happy Coding!

Share This Article:


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

April 22, 2011

2

VBScript Get File Extension

Before anything else, you may love to read this book too!...


Actually, I got this question from the reader of my previous blog post about VBScript to Replace Underscores in Filename with Spaces for All Files in a Folder, and I decided to answer it here.

Question from Justin:
I tried something simular..but modified your VBS..
'========================================================
' VBScript to replace underscore in file name with space
' for each files in a folder
' Written by ApOgEE of http://coderstalk.blogspot.com
'========================================================
Dim sName
Dim fso
Dim fol

' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

' get current folder
Set fol = fso.GetFolder(".")

' go thru each files in the folder
For Each fil In fol.Files
' check if the file name contains underscore
If InStr(1, fil.Name, "The ") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, "The ", "") & ", The"
' rename the file
fil.Name = sName
End If
Next

' echo the job is completed
WScript.Echo "Completed!"

i have Files in a given folder that has Names in them...

ie.
The Last Star Fighter
Gone with the wind
The Good, the Bad, and the Ugly.

With the modified VBS above Ive run it and i get the following...
Last Star Fighter.mp4, The
Gone with the Wind
Good, the Bad, and the Ugly.mp4, The
How do i get rid of the extention in the middle of the name? What am I doing wrong?

My First answer:
Dear Justin,
check your code again. Search for line of code containing
"sName = Replace"
and remove the code after ')'.

Specifically, the code:
'& ", The"'
have to be removed.

Justin Replies:
Ok i Changed the Line in question...and Reran the code...

Now i get...
Gone with the Wind --ok
Good, the Bad, and the Ugly --Problem

Last Star Fighter --Problem

They Should read...
Gone with the Wind
Good, the Bad, and the Ugly, The
Last Star Fighter, The

My Answer:
If that is the case, you should get and strip the file extension before you replace the name and add it back to the filename after replacement. Here is the snippet:
' get the file extension
sExtension = fso.GetExtensionName(fil.Name)
' strip the extension from file name
sName = Replace(fil.Name, "." & sExtension, "")
' replace the whatever and put the extension back
sName = Replace(sName, "The ", "") & ", The" & "." & sExtension

and your new code should look like this:
'========================================================
' VBScript to replace underscore in file name with space
' for each files in a folder
' Written by ApOgEE of http://coderstalk.blogspot.com
'========================================================
Dim sName
Dim fso
Dim fol

' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

' get current folder
Set fol = fso.GetFolder(".")

' go thru each files in the folder
For Each fil In fol.Files
' check if the file name contains underscore
If InStr(1, fil.Name, "The ") <> 0 Then
' replace underscore with space
' sName = Replace(fil.Name, "The ", "") & ", The"
' get the file extension
sExtension = fso.GetExtensionName(fil.Name)
' strip the extension from file name
sName = Replace(fil.Name, "." & sExtension, "")
' replace the whatever and put the extension back
sName = Replace(sName, "The ", "") & ", The" & "." & sExtension
' rename the file
fil.Name = sName
End If
Next

' echo the job is completed
WScript.Echo "Completed!"
Good luck!

Share This Article:


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy




April 21, 2011

1

HitungHari - PHP Countdown Banner Application


The ideas developed since my previous Countdown Banner for MOSC2011. I have made some modification on the source code and then it spark me an idea to share the source code as open source too. Therefore, I clean up my messy code, make some documentation and comments so people can reuse the code. And I name it as HitungHari.

The word HitungHari actually come from Malay words for counting days. Where 'Hitung' means 'Count', and 'Hari' means 'Days'. I have more ideas to develop from this piece. Perhaps, you may also have ideas too. I would be glad to hear your comments. Checkout the Google Code site for HitungHari and feel free to download, try, hack, and comment it.

Share This Article:


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

April 19, 2011

2

MOSC 2011 Countdown Banner


I'm just having fun with PHP GD and Inkscape. So, to make my toy useful, I would like to dedicate this Countdown banner for the upcoming MOSC 2011 event. You are free to put it on your blog or website. Here is the code to put on your site:

<!-- MOSC2011 Countdown Banner By ApOgEE - code start -->
<div style="width:100%; text-align:center;">
<a href="http://www.mosc.my" target="_blank">
<img src="http://www.apogeek.com/osdc/mosc-countdown-img.php" target="_blank">
</a></div>
<!-- MOSC2011 Countdown Banner By ApOgEE - code end -->

And here is the result:


I hope you enjoy it! Feel free to Follow this blog if you like it.

Update:
I heard request on MOSC2011 mailing list where some of them want this banner in different sizes. So, I've modified my php code and add this sizes:

MOSC Countdown Banner 300x250
<!-- MOSC2011 Countdown Banner By ApOgEE - code start 300x250 -->
<div style="width:100%; text-align:center;">
<a href="http://www.mosc.my" target="_blank">
<img src="http://www.apogeek.com/osdc/mosc-countdown-img.php?size=1" target="_blank">
</a></div>
<!-- MOSC2011 Countdown Banner By ApOgEE - code end -->

MOSC Countdown Banner 728x90
<!-- MOSC2011 Countdown Banner By ApOgEE - code start 728x90 -->
<div style="width:100%; text-align:center;">
<a href="http://www.mosc.my" target="_blank">
<img src="http://www.apogeek.com/osdc/mosc-countdown-img.php?size=2" target="_blank">
</a></div>
<!-- MOSC2011 Countdown Banner By ApOgEE - code end -->

I follow 2 of Google Adsense size. There are more wich I haven't had time to do yet. Please tell me if you need other specific size.

Latest Update:
I have shared my source code for this Countdown Banner and I call it HitungHari.

Share This Article:


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy




April 13, 2011

0

Safe Way To Learn Chemical Reaction Without Using Actual Chemicals

I played with hydrochloric acid during my school's chemistry lab session. Drop a little amount of it to my text book while my teacher is teaching something else, and see the pages being eaten by those acids. Just because I'm getting bored in class. That was one case of myself. How about you? Have you ever misuse your science lab chemicals? Today, I found other way for student to learn about chemical reaction without ever use the actual chemical.



At Museum of Science and Industry in Chicago, you can experiment and create a chemical reaction using their interactive exhibit in the Science Storms wing. This interactive exhibit is called Create a Chemical Reaction. Using specially-tagged pucks, visitors can grab atoms from the periodic table and combine them to cause chemical reactions.



The "Create a Chemical Reaction" interactive exhibit is powered by Sensetable platform from Patten Studio. The Sensetable concept and initial prototypes were developed by the Tangible Media Group at the MIT Media Lab. James Patten, Hiroshi Ishii, Jim Hines and Gian Pangaro are the people who invent this amazing Sensetable, A Wireless Object Tracking Platform for Tangible User Interfaces. Iwish I could play with it. I can imagine how this great invention can change our computing interface in the near future.

(Via Patten Studio)

Share This Article:


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy