#!/usr/bin/python # connect.py - connect to the MySQL server importsysimportMySQLdbtry:conn=MySQLdb.connect(db="cookbook",host="localhost",user="user",passwd="passwd",unix_socket="/var/lib/mysql/mysql.sock")print"Connected"exceptMySQLdb.Error,e:print"Cannot connect to server"print"Error code:",e.args[0]print"Error message:",e.args[1]sys.exit(1)conn.close()print"Disconnected"