Categories
linux

Ubuntu 12/13 install and configure Java JRE

1. Download JRE 7 from Oracle website.
2. Unarchive the file:
[code language=”bash”]
tar zxpvf jre-7u25-linux-x64.tar.gz
[/code]
3. Create the installation directory
[code language=”bash”]
sudo mkdir -p /usr/lib/jvm/
[/code]
4. Move the unarchived directory in the newly created one
[code language=”bash”]
sudo mv ./jre1.7.0* /usr/lib/jvm/jre1.7.0
[/code]
5. execute this wonderful command:
[code language=”bash”]
sudo update-alternatives –install /usr/bin/java java /usr/lib/jvm/jre1.7.0/bin/java 3
[/code]
Done.
You can see the java version doing:
[code language=”bash”]
java -version
[/code]

KISS

Categories
Programming

Web server in one line of bash

If you want to quickly save a file through http but you don’t want to install a web server, you can just use netcat.
You can run:
[code language=”bash”]
while true; do { echo -e ‘HTTP/1.1 200 OK\r\n’; cat index.html; } | nc -l 8080; done
[/code]

index.html can be any file you want to serve it.

You can access it after that as: http://host_ip:8080/

Categories
python

Example for singleton decorator pattern in python

I know there is not very common in Python to use the singleton pattern, but I found a nice implementation of this pattern in Python 3 Patterns, Recipes and Idioms book. Starting with that example I implemented an equivalent of the well known PHP getConnection example.

You have the code below:

This is the class that implements the Singleton pattern.

[code language=”python”]
class Singleton:
def __init__(self, klass):
self.klass = klass
self.instance = None
def __call__(self, *args, **kwds):
if self.instance == None:
self.instance = self.klass(*args, **kwds)
return self.instance
[/code]

Now, we create a class and we decorate it with the Singleton class. Let’s import also MySQLdb module*.

[code language=”python”]
import MySQLdb

@Singleton
class Database:
connection = None
def get_connection(self):
if self.connection is None:
self.connection = MySQLdb.connect(host="localhost", user="root", passwd="razvan", db="mydatabase")
return self.connection
[/code]

Let’s test this:

[code language=”python”]
db1 = Database().get_connection()
db2 = Database().get_connection()

print (db2)
print (db1)
[/code]

You will see something like:

[code language=”bash”]
<_mysql.connection open to ‘localhost’ at 16b4800>
<_mysql.connection open to ‘localhost’ at 16b4800>
[/code]

As you can see there is only one object.

For fun, let’s remove the line “@Singleton” and re-run the example. This time you will see different objects:

[code language=”bash”]
<_mysql.connection open to ‘localhost’ at c91e20>
<_mysql.connection open to ‘localhost’ at bccba0>
[/code]

You can find the fully example here.

* If you don’t know how to install MySQLdb, you can check our previous post.

Categories
Programming python

Install MySQLdb for Python 2.x

If you want to install MySQLdb for Python 2.x, without using the package manager of your Linux distro (Ubuntu in our case), you can do it through pip like this:

First you must to have installed libmysqlclient-dev (and, of course pip)

[source language=”bash”]
sudo apt-get install libmysqlclient-dev
[/source]

After that execute this two commands:

[source language=”bash”]
sudo easy_install -U distribute
sudo pip install mysql-python
[/source]

Test if is working:
Type in the command line:

[source language=”bash”]
python
[/source]

and in the python console:

[source language=”python”]
>>> import MySQLdb
>>> help(MySQLdb)
[/source]

That’s all. Enjoy!