Sunday Feb 5

Archive for the ‘Python’ Category

Jan
03/12
Python 2.7 on CentOS 5.6
Last Updated on Tuesday, 10 January 2012 01:52
Written by Cody Snider
Tuesday, January 3rd, 2012

Here you have it, folks. A shell script that will handle all the ugliness associated with installing Python 2.7 on CentOS 5.6 (and probably CentOS 5.7, too).

Use the following command in your shell (the easy way):

wget -q -O http://codingwithcody.com/downloads/python_27_on_centos_56/install.sh | sh

Alternately, you can download and run it manually: Download install.sh

Here are the tarballs used just in case the servers hosting them go down and you’re eager to get Python 2.7 working:

And, just FYI, here’s the contents of install.sh:

yum -y install gcc gdbm-devel readline-devel ncurses-devel zlib-devel bzip2-devel sqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel make
 
cd /var/tmp
wget http://sqlite.org/sqlite-amalgamation-3.7.3.tar.gz
tar xfz sqlite-amalgamation-3.7.3.tar.gz
cd sqlite-3.7.3/
./configure
make
make install
 
cd /var/tmp
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
tar xvfz Python-2.7.1.tgz
cd Python-2.7.1
./configure --prefix=/opt/python2.7.1 --with-threads --enable-shared
make
make install
 
touch /etc/ld.so.conf.d/opt-python2.7.1.conf
echo "/opt/python2.7.1/lib/" >> /etc/ld.so.conf.d/opt-python2.7.1.conf
ldconfig
 
ln -sf /opt/python2.7.1/bin/python /usr/bin/python2.7
 
cd /var/tmp
wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg --prefix=/opt/python2.7.1
 
/opt/python2.7.1/bin/easy_install pip
ln -sf /opt/python2.7.1/bin/pip /usr/bin/pip
 
pip install virtualenv
ln -sf /opt/python2.7.1/bin/virtualenv /usr/bin/virtualenv
 
mv /usr/bin/python /usr/bin/python-backup
ln -s /usr/bin/python2.7 /usr/bin/python

Any improvements are more than welcome. Enjoy and happy coding!

Posted under Python, Servers  |  Comments  3 Comments
May
17/10
Generate Random IP with Python
Last Updated on Saturday, 3 July 2010 03:43
Written by Cody Snider
Monday, May 17th, 2010

In need of an IP address on-the-fly that appears to be valid? Try this:

View Code PYTHON
from random import randrange
 
def generateIP():
    blockOne = randrange(0, 255, 1)
    blockTwo = randrange(0, 255, 1)
    blockThree = randrange(0, 255, 1)
    blockFour = randrange(0, 255, 1)
    print 'Random IP: ' + str(blockOne) + '.' + str(blockTwo) + '.' + str(blockThree) + '.' + str(blockFour)
    if blockOne == 10:
        return self.__generateRandomIP__()
    elif blockOne == 172:
        return self.__generateRandomIP__()
    elif blockOne == 192:
        return self.__generateRandomIP__()
    else:
        return str(blockOne) + '.' + str(blockTwo) + '.' + str(blockThree) + '.' + str(blockFour)

We’re skipping 10.x.x.x, 172.x.x.x and 192.x.x.x due to the fact that these are reserved address. RFC 1918

Version 2:

An elegant solution to serve the purpose of generating a random IP provided by Ben (explanation of changes listed in the comments below):

View Code PYTHON
from random import randrange
if __name__=="__main__":
    not_valid = [10,127,169,172,192]
 
    first = randrange(1,256)
    while first in not_valid:
    first = randrange(1,256)
 
    ip = ".".join([str(first),str(randrange(1,256)),
    str(randrange(1,256)),str(randrange(1,256))])
    print ip
Posted under Python, SEO  |  Comments  6 Comments