Posts Tagged Debian Lenny
23Oct/090
mod_python and MySQLdb
How to install mod_python and MySQLdb in Debian Lenny?MySQL is one of greatest databases for web sites, and for applications and it's probably most used with PHP. Python is a good language for a lot of things. In my case, I needed some daemons, to do certain tasks. Firstly, I tried with PHP, but it is language for web pages, and it don't perform well as a daemon. So then I have to choose which language to use. C++ is powerful but very complex and there are other languages, that could fit me better for this project. I don't have a lot of idea about Perl and so I tried programming Python.
I faced two problems in last two days:
1. I had problems with installing MySQLdb
2. How to make and publish a simple website in Python?
MySQLdb
I got a few errors while trying to install MySQLdb by using python file.py install. As far as I searched the Internet, there are some workarounds but neither of them worked for me. And then it hit me. Doesn't Debian comes with apt, a great package tool?
Installing this plug-in is very easy with apt. You just need to run following command:
apt-get install python-mysqldb
And now you can use MySQL from Python.
Example:
#! /usr/bin/python
import MySQLdb
import sys #for exit
try:
conn = MySQLdb.connect(host="localhost", user="root", passwd="", db = "database", charset = "utf8", use_unicode = True)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
cursor = conn.cursor()
cursor.execute("SET NAMES utf8;SET CHARACTER SET utf8;SET character_set_connection=utf8;")
cursor.close()
cursor = conn.cursor()
while(1):
cursor.execute("SELECT * FROM table")
if cursor.rowcount > 0:
rows = cursor.fetchall()
for row in rows:
if row == None:
break
print "Data: %s %s" % (row[0], row[1])
This is an example for UTF-8 charset encoding. I still don't know why I needed to close cursor after executing SET NAMES utf8, but this code should work without a problem.
After my daemon feetched all needed data, I need to display it through web browser. PHP came to my mind, but if one thing is in Python, why not make other thing to? So for that I needed mod_python.
How to install mod_python and use it?
It can be installed by typing following command:
apt-get install libapache2-mod-python
You need to put following code in .htaccess or your Apache's configuration file to use Python:
AddHandler mod_python .py
PythonHandler mod_python.publisher
Now just write Python code, save it as something.py and put it in your website's root.
28Sep/090
Install Sun Java on Debian Lenny
Installing Java on Windows is easy. Simply download .exe file and run it. But on Debian Linux it's a little bit harder because it's non-free software.It's free as in beer. So you can actually "taste" it, but cannot get the source code.
How to install Sun Java on Debian Lenny?
1. Open Terminal and login as root (type "su" and then type a password)
2. Type:
nano /etc/apt/sources.list
3. In file change:
deb http://ftp.uk.debian.org/debian/ lenny main
deb-src http://ftp.uk.debian.org/debian/ lenny main
to:
deb http://ftp.uk.debian.org/debian/ lenny main non-free
deb-src http://ftp.uk.debian.org/debian/ lenny main non-free
Save it.
4. Refresh aptitude packages by typing:
apt-get update
5. Type:
apt-get install sun-java6-jre
to install Java 6 from Sun.
26Sep/090
Screenshot of Web Page on Linux Server
How to make a screenshot of web page on Linux server?In this tutorial I will tell how to setup a server with Debian Lenny operating system to add capability for taking screenshot pictures of a web page you want.
Steps:
1. Login to server through SSH as root user.
2. Install vnc server, web browser and GUI with following command:
apt-get install tightvncserver xfonts-base xfce4 iceweasel imagemagick
It’s about 160MB to download and installation should take a few minutes.
3. Start VNC server by typing:
vncserver :1 -geometry 1024x768 -depth 24
4. Server should be running now, and you can stop some of probably unwanted services:
killall x-session-manager ssh-agent xftaskbar4 xfdesktop xfce4-panel
5. Now connect to the VNC server from your desktop machine and remove unnecessary panels, configure Iceweasel web browser.
If you’re using iptables firewall you’ll need to add following rule to allow access to VNC server:
-A INPUT -p tcp -m tcp --dport 5901 -j ACCEPT
6. That’s it. Now you have a machine capable of taking screenshots of websites.
How to take a screenshot?
Create a shell file (for example screenshot.sh) with following script which starts the web browser, loads a page, takes a picture and closes the web browser.
#!/bin/bash
export DISPLAY=":1"
/usr/bin/iceweasel --display :1 "$1" > /dev/null 2> /dev/null &
/bin/sleep 10
/usr/bin/import -window root -display :1 "$2"
killall -9 /usr/lib/iceweasel/firefox-bin
This script can be called like this:
./screenshot.sh http://www.google.com /var/www/picture_path.jpg
Now you can take picture of any page you’d like with this simple command. It takes about 11 seconds to take 1 screenshot (script needs to sleep for about 10 seconds so that the page can load in web browser - you can change that time but is not recommended to set it bellow 5 seconds because most of the pages won’t load that quickly).
When you’re finished with taking screenshots you can kill vncserver with following command:
vncserver -kill :1
That's it for this tutorial. Additionally you can write PHP script which calls screenshots.sh and automatically crop picture to remove sliders and other unwanted elements on it.
