Monday, October 18, 2010

running a simple embedded servlet container with Jetty

1. Install MySQL for Python

a. Download and install Jetty from http://dist.codehaus.org/jetty/jetty-6.1.25/jetty-6.1.25.zip

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");
server.start();
}
}



b. HelloWorld servlet code : HelloWorldServlet.java

package tkang.test;

import java.io.IOException;

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 {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("

Hello

");
}

}

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 http://pypi.python.org/pypi/setuptools#files


[root@skcc-nebdap37 src]# wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e

[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 setup.py build

[root@skcc-nebdap37 src]# python setup.py install


c. Download and install MySQL for Python from http://sourceforge.net/projects/mysql-python


2. Sample MySQL for Python code


#!/usr/bin/python

import MySQLdb

# Establich a connection
db = MySQLdb.connection(host="localhost",
user="nebula",
passwd="nebula",
db="nebula")

# 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 : http://www.lag.net/paramiko/download/paramiko-1.7.6.tar.gz
- paracrypto : http://www.amk.ca/files/python/crypto/pycrypto-2.0.1.tar.gz
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.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=addr, username="root", password="rootpw")

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

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

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

client.close()