Working with PythonIn order to work with Python virtual server has to have application type Python (mod_wsgi) selected, script should be executable (500) and should have extension *.wsgi
db_server — database server address for your account. Can be retrieved in service Control Panel. db_login — database user for connection, is being created simultaneously with database db_pass — password for database connection, is being set up on database creation. You can always retrieve it in corresponding section of service Control Panel. db_name — database name - equal to $db_login
Connecting to MySQL
# -*- coding: utf-8 -*-
import MySQLdb
db_server = 'db*.valuehost.ru' db_login = 'adminlogin_test' db_pass = 'MsBs1sT0' db_name = 'adminlogin_test'
def application(environ, start_response):
status = '200 OK'
output = db_example()
response_headers = [('Content-type', 'text/html;charset=UTF-8'),('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
def db_example():
try:
dbh = MySQLdb.connect(db_server,db_login,db_pass,db_name)
except MySQLdb.Error, error:
return 'Could not connect to database server: '+error.args[1]
cursor = dbh.cursor()
query = 'SELECT VERSION()'
try:
cursor.execute(query)
except MySQLdb.Error, error:
return 'Could not exequte query ('+query+'): '+error.args[1]
row = cursor.fetchone()
cursor.close()
return 'Server version MySQL: '+row[0]
Connecting to PostgreSQL
# -*- coding: utf-8 -*-
from pyPgSQL import PgSQL
db_server = 'db*.valuehost.ru' db_login = 'adminlogin_test' db_pass = 'MsBs1sT0' db_name = 'adminlogin_test'
def application(environ, start_response):
status = '200 OK'
output = db_example()
response_headers = [('Content-type', 'text/html;charset=UTF-8'),('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
def db_example():
try:
dbh = PgSQL.connect(db_name,db_login,db_pass,db_server)
except PgSQL.DatabaseError, error:
return 'Could not connect to database server: '+error.args[0]
cursor = dbh.cursor()
query = 'SELECT VERSION()'
try:
cursor.execute(query)
except PgSQL.DatabaseError, error:
return 'Could not execute query ('+query+'): '+error.args[0]
row = cursor.fetchone()
cursor.close()
return 'Server version PostgreSQL: '+row[0]
|
|