Monday, October 18, 2010

running a simple embedded servlet container with Jetty

1. Install MySQL for Python

a. Download and install Jetty from

b. unzip the pkg.
[root@skcc-nebdap37 src]# yum install mysql mysql-devel

c. include the following jar in Java build path
- jetty-6.1.25.jar
- jetty-util-6.1.25.jar
- servlet-api-2.5-20081211.jar

2. Java code for running a servlet on a Jetty

a. main()

package tkang.test;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

public class JettyTest {

public static void main(String args[]) throws Exception {
Server server = new Server(8080);
Context root = new Context(server,"/",Context.SESSIONS);
ServletHolder sh = new ServletHolder(HelloWorldServlet.class);
root.addServlet(sh, "/helloworld");

b. HelloWorld servlet code :

package tkang.test;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {

public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {




Sunday, October 17, 2010

mysql client with python

1. Install MySQL for Python

a. install mysql and mysql-devel pkg

[root@skcc-nebdap37 src]# yum install mysql mysql-devel

b. install python setuptools from

[root@skcc-nebdap37 src]# wget

[root@skcc-nebdap37 src]# gunzip setuptools-0.6c11.tar.gz

[root@skcc-nebdap37 src]# tar xvf setuptools-0.6c11.tar

[root@skcc-nebdap37 src]# cd setuptools-0.6c11

[root@skcc-nebdap37 src]# python build

[root@skcc-nebdap37 src]# python install

c. Download and install MySQL for Python from

2. Sample MySQL for Python code


import MySQLdb

# Establich a connection
db = MySQLdb.connection(host="localhost",

# Run a MySQL query from Python and get the result set

db.query("""select * from USERS""")
r = db.store_result()

# Iterate through the result set
# Example calls back up to 100 rows
for row in r.fetch_row(100):
print row

Thursday, October 14, 2010

ssh client with python

1. Install paramiko & paracrypto
- paramiko :
- paracrypto :
NOTE : they all work on a python version >= 2.4

2. sample code for using paramiko
- the following python code checks if sshd is alive at a remote server.

import paramiko
import sys
import socket
import os

addr = sys.argv[1]

client = paramiko.SSHClient()
client.connect(hostname=addr, username="root", password="rootpw")

stdin, stdout, stderr = client.exec_command("ps -ef | grep sshd")

found = False
for line in
if line.find("/usr/bin/sshd"):
found = True

if found:
print "sshd is alive!"
print "sshd is dead!"