[Tutor] Python and PHP config files
Michael A. Peters
mpeters@mac.com
Sat, 1 Jun 2002 02:31:12 -0700
Hi all-
I'm trying to have python run as a cron in the background to assist in a
php web application.
I need to have the python script be able to get values from a php config
file.
Here's the relative parts of my script so far-
-------------------------------
#!/usr/local/bin/python
PathToConfig = "/var/www/folding_stats/folding.config.php"
import os, sys, urllib
try:
import MySQLdb
except ImportError, e:
print "It seems you do not have the python MySQLdb module installed."
sys.exit(1)
if os.path.isfile(PathToConfig):
# here is where I read the data I need
datafile = open(PathToConfig)
config = datafile.readlines()
datafile.close()
else:
print "I can not find your config file."
print "Please specify its location at"
print "line 2 of this file."
sys.exit(1)
-------------------------------
config is now an array containing the php file- but I need to extract info
out of that, and I can't seem to figure out how :(
For example, the lines in the file i need are-
$db_name="foldingathome";
$db_host="localhost";
$db_port="3306";
$db_user="samwise";
$db_password="xyzzy";
I need to find where the php variable for db_name is defined and assign a
python variable for that- such as
db_name = somefunction(config, db_name)
where
somefunction(config, db_name)
would find $db_name= in the config list and then return what's in their.
Anyone know how to do it?
in bourne with shell tools I would do
db_name=`grep "^\$db_name=" filename |cut -d"\"" -f2`
but I'm using python, not bourne :D
Thanks for any help