Category > Uncategorized

Bits of python: import a CSV file into a MySQL database.

» 22 February 2012 » In Uncategorized » 1 Comment

This is my first blog post of the series “Bits of python”, a journey into the Python programming language done with simple examples. From time to time I’ll write about Python and Django and how they interact with Unix and Linux systems. All the scripts available are stripped versions of my daily work, mostly file parsers, databases administration and reporting tools. This first example is about how to import a CSV (Comma-separated values) files into a MySQL database and can be very handy when you work with spreadsheets and databases.

The python modules used in this example are, the CSV  and the python-mysqldb, the first is already present into python and for the second one you can install it just with apt-get install python-mysqldb.

The sample code consist in three files, presidents.csv, presidents.sql and importCSV.py, can be downloaded from here, once downloaded the archive can be unpacked with tar xvfz csvintomysql.tar.gz . Now it’s time to take a look to our file, let’s go into csvintomysql directory, cd csvintomysql.

The presidents.csv file is the list of all the presidents of the United States of America and it look like this (the list goes more long):

Presidency , President, Took office, Left office, Party, Home State

1,George Washington,1789-04-30,1797-03-04,Independent ,Virginia
2,John Adams,1797-03-04,1801-03-04,Federalist ,Massachusetts
3,Thomas Jefferson,1801-03-04,1809-03-04,Democratic-Republican ,Virginia
4,James Madison,1809-03-04,1817-03-04,Democratic-Republican ,Virginia
5,James Monroe,1817-03-04,1825-03-04,Democratic-Republican ,Virginia

As you can see there are several fields delimited by a comma that need to be exported and inserted into the Mysql database using the python CSV module.

The presidents.sql file is the series of MySQL instruction to create a database called csvdb and a table called presidents. In order to keep simple the creation of the database you need to log into MySQL as root and from the database prompt type:


mysql> source presidents.sql
Query OK, 1 row affected (0.00 sec)

Database changed
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

The importCSV.py is the Python script that parse and insert the CSV data into the database, the connection credential are already configured to connect to the database csvdb with csv as user and csv as password. Please note, if you want to modify the credential you need to change the settings in the first place, precisley in the presidents.sql file. Do not use this script in production environment!!!!! till you changed the Mysql login credential for the csv user.

#!/usr/bin/python
#simple python csv parser that export Comma
#Separated Value data into a MySQL database.
#phillip@bailey.st
import csv
import MySQLdb
# open the connection to the MySQL server.
# using MySQLdb
mydb = MySQLdb.connect(host='localhost',
    user='csv',
    passwd='csv',
    db='csvdb')
cursor = mydb.cursor()
# read the presidents.csv file using the python
# csv module http://docs.python.org/library/csv.html
csv_data = csv.reader(file('presidents.csv'))
# execute the for clicle and insert the csv into the
# database.
for row in csv_data:

    cursor.execute('INSERT INTO PRESIDENTS(PRESIDENCY ,PRESIDENT \
            ,TOOKOFFICE ,LEFTOFFICE ,PARTY ,HOMESTATE)' \
            'VALUES(%s, %s, %s, %s, %s, %s)',  row)
#close the connection to the database.
cursor.close()
print "Import to MySQL is over"

Line 5 and 6 are the import module declaration.

Line 9 to 12 are the MySQL connection credential.

Line 13 prepare the cursor to handle the connection.

Line 16 the csv.reader will read and parse the presidents.csv file.

Line 19 a for loop will be performed.

Line 21 will execute the insert into PRESIDENTS table query.

Line 25 will close the database connection.

You can run the script with:

root@:# python importCSV.py
Import to MySQL is over

After the

mysql> use csvdb;

select * from PRESIDENTS;


+----+---------------------+------------+-----------------------------+------------+------------+-------------------------------------------+---------------+
| id | cur_timestamp       | PRESIDENCY | PRESIDENT                   | TOOKOFFICE | LEFTOFFICE | PARTY                                     | HOMESTATE     |
+----+---------------------+------------+-----------------------------+------------+------------+-------------------------------------------+---------------+
|  1 | 2012-02-22 23:45:05 | 1          | George Washington           | 1789-04-30 | 1797-03-04 | Independent                               | Virginia      |
|  2 | 2012-02-22 23:45:05 | 2          | John Adams                  | 1797-03-04 | 1801-03-04 | Federalist                                | Massachusetts |
|  3 | 2012-02-22 23:45:05 | 3          | Thomas Jefferson            | 1801-03-04 | 1809-03-04 | Democratic-Republican                     | Virginia      |
|  4 | 2012-02-22 23:45:05 | 4          | James Madison               | 1809-03-04 | 1817-03-04 | Democratic-Republican                     | Virginia      |
|  5 | 2012-02-22 23:45:05 | 5          | James Monroe                | 1817-03-04 | 1825-03-04 | Democratic-Republican
+----+---------------------+------------+-----------------------------+------------+------------+-------------------------------------------+---------------+

I hope the example will be useful to understand how to work with spreadsheets and CSV data sets. If you have doubts or questions, please leave a comment on the post. All the code is freely available at http://bailey.st/code/bitsofpy/csvintomysql/

Best.

Did you like this? Share it:
Scridb filter

Continue reading...

Tags:

GPA GNU Privacy Assistant on Xubuntu/Ubuntu 11.10 32 and 64 bits.

» 15 February 2012 » In Uncategorized » 1 Comment

Recently I found some repositories where to get the GNU Privacy Assistant .deb packages suitable for Xubuntu/Ubuntu 11.10 32 and 64 bit. Maybe GPA is a bit outdate compared to Enigmail, but around there’s still people that like this GTK interface. Below you can find the steps to install it on a 64 or a 32 bit Ubuntu like system.

Xubuntu 64 bit


https://launchpad.net/ubuntu/oneiric/amd64/gpa/0.9.0-1

wget http://launchpadlibrarian.net/37721679/gpa_0.9.0-1_amd64.deb

dpkg -i gpa_0.9.0-1_amd64.deb

apt-get -f install

Xubuntu 32 bit


https://launchpad.net/ubuntu/oneiric/i386/gpa/0.9.0-1

wget http://launchpadlibrarian.net/37717999/gpa_0.9.0-1_i386.deb

dpkg -i gpa_0.9.0-1_i386.deb

apt-get -f install

After the installation the GPA can be found in Applications Menu > Accessories > GNU Privacy Assistant.

Did you like this? Share it:
Scridb filter

Continue reading...

Tags:

Openwrt LuCI web interface SSL management on the WAN interface.

» 08 February 2012 » In Uncategorized » 3 Comments

Openwrt is a Linux firmware that can transform your home wifi router in a powerful network device, with more than 2000 software packages you can have out of the box a firewall, a voip gateway, a VPN server along with many other functionalities. In this short post I’m going to explain how to enable the SSL and the SSH management on the WAN port with the latest OpenWRT firmware (Backfire 10.03.1-RC6).

Assuming your OpenWRT box have a local ip address 192.168.1.1, you can ssh into it with ssh 192.168.1.1 -l root .

Once you’ve logged in run the following commands to install the SSL support for the LuCI web interface.

opkg update
opkg install luci-ssl
/etc/init.d/uhttpd restart

In the /etc/firewall.user file add the following line

iptables --append input_wan --protocol tcp --dport 443 --jump ACCEPT

Please restart the firewall, otherwise the new rule won’t take effect.

/etc/init.d/firewall restart

Now you are ready to log into your OpenWRT router using the htts://wanaddress .

Enjoy.

Did you like this? Share it:
Scridb filter

Continue reading...

Tags:

Turnkey-Linux, get running your favourite web apps in minutes.

» 04 December 2011 » In Uncategorized » No Comments

The Turnkey Linux is an amazing project of various ready-to-use web applications, 45+ virtual appliances are packaged in multiple build formats, from VMDK disk image with OVF support and an installable Live CD which can be installed on bare metal and virtual machines. The most common appliances provided by Turnkey are LAMP server, Joomla, Drupal, WordPress, MySQL, MediaWiki, Domain controller, File server, Ruby on Rails, phpBB. Non official Turnkey Linux appliances are available on the web, such Sahana-Eden, LimeSurvey, Ampache along with the two intrusion detection solutions Insta-snorby and Smooth-Sec. In this how to, I’m going to show how to install from scratch  the Redmine Turnkey Linux appliance on a Virtualbox headless Server within minutes. If wondering how to install a Headless Virtualbox server, please check my related blog post on “phpvirtualbox, manage your virtual machines from anywhere.

1) New Virtual Machine (choose a name)


2) Allocate the VM memory (512 MB will be fine)

3) New virtual hard disk (Create a new disk)

4) Type of the virtual disk (select the VDI, virtualbox default)

5) Virtual disk allocation (dynamically will be fine in most of the cases)

6) Virtual disk size . (This is up to you)

7) Last chance to edit the disk settings.

8) Attach the Turnkey Iso appliance.

9)  Start the VM and begin with the installation.

10) Disk partitioning.

11) Commit the disk changes.

12) Installation complete – Reboot the system.

13) Enter the new root password.

14) Enter the Mysql root password.

15) Enter the redmine admin password.

16) Enter the redmine admin email address.

17) Once arrived at the confconsolle you can manage your appliance.

18) Point the web browser to the appliance ip address and login with the credentials
previously inserted.

If want to experiment and try other appliances, please visit the turnkeylinux.org and get your appliance and don’t forget to give a shout-out to to Alon Swartz and Liraz Siri.

Did you like this? Share it:
Scridb filter

Continue reading...

Tags:

Smooth-Sec version 1.3 is out

» 25 November 2011 » In Uncategorized » No Comments

Yesterday I’ve released a new version (1.3) of Smooth-Sec, major improvements are Snorby upgraded to version 2.3.11 and Suricata upgraded to version 1.1 stable, compiled with with NFQ support.

Download here:
https://sourceforge.net/projects/smoothsec/files/SmoothSec-1.3/

For a better communication I’ve set up a Smooth-Sec mailing list, where everyone can share tips/tricks, ideas and issues related to Smooth-Sec.

https://lists.sourceforge.net/lists/listinfo/smoothsec-talk

Did you like this? Share it:
Scridb filter

Continue reading...

Tags: