On 18/01/11 1:20 PM, Watson Xu wrote:
Hi,

I created a PHP page and one Python script. Please see the following in
blue for the PHP source code and the Python script. When I launch the
PHP page in IE to start the python script, the python script give me the
error information shown as blow, please see the following error
information in yellow. Could you give me some suggestions for it? Thanks
a lot!

BTW, this Python script work fine when I execute it on Linux server with
“root” manually.

*PHP source code:*

*----------------------*

<?

$result=exec("/usr/bin/python /var/www/html/test.py");

print $result;

?>

*Python Script (*/var/www/html/test.py*):*

--------------------

#!/usr/bin/python

#Created by Watson Xu at 2011-01-06

import smtplib

from email.MIMEText import MIMEText

import re, os, sys, time

import MySQLdb

MySQLdbHost="yyy.yyy.yyy.yyy"

MySQLdbUser="yyy"

MySQLdbPassword="yyy"

MydbSID="yyy"

MydbPort=3306

SQLQuery='SELECT servername FROM ping_status WHERE pingstatus="failed"'

mailTime=time.strftime("%Y-%m-%d %X", time.localtime())

mailto_list='wat...@hf.webex.com'

mail_me="xx...@hf.webex.com"

mail_host="xxx.xxx.xxx.xxx"

mail_user="xxx"

mail_pass="xxx"

mail_postfix="xxx.xxx"

mail_title='xxxxxxx'

mail_body=""

def send_mail(to_list,sub,content):

me=mail_me

msg = MIMEText(content)

msg['Subject'] = sub

msg['From'] = me

msg['To'] = mailto_list

try:

s = smtplib.SMTP()

s.connect(mail_host)

s.login(mail_user,mail_pass)

s.sendmail(me, to_list, msg.as_string())

s.close()

return True

except Exception, e:

print str(e)

return False

##select host from MySQL DB

Myconn=MySQLdb.connect(host=MySQLdbHost, user=MySQLdbUser,
passwd=MySQLdbPassword, db=MydbSID, port=MydbPort)

Mycursor=Myconn.cursor()

Mycursor.execute(SQLQuery)

results=Mycursor.fetchall()

for i in range(len(results)):

pingHost=results[i][0]

mail_body=mail_body+"\n"+pingHost+"\n"

i=i+1

if send_mail(mailto_list,mail_title,mail_body):

print "Send mail success......"

else:

print "Send mail failed......"

*Error information from Python script:*

-------------------------------------------------

Traceback (most recent call last):

File "/var/www/html/test.py", line 67, in ?

Myconn=MySQLdb.connect(host=MySQLdbHost, user=MySQLdbUser,
passwd=MySQLdbPassword, db=MydbSID, port=MydbPort)

File "/usr/lib/python2.4/site-packages/MySQLdb/__init__.py", line 75, in
Connect

return Connection(*args, **kwargs)

File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line
164, in __init__

super(Connection, self).__init__(*args, **kwargs2)

_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL
server on '10.224.71.99' (13)")

Thanks,

Watson



_______________________________________________
DB-SIG maillist  -  DB-SIG@python.org
http://mail.python.org/mailman/listinfo/db-sig

The first thing I suggest you do is to decouple the Python script from the PHP page. That will eliminate any permission issues you may be having.

Then try and execute the parts of your script that look to be causing the error in the Python interactive interpreter. At a quick glance you should be able to limit this to something like

>>> import MySQLdb
>>> MySQLdbHost="yyy.yyy.yyy.yyy"
>>> Myconn=MySQLdb.connect(host="yyy.yyy.yyy.yyy", user="yyy", passwd="yyy", db="yyy", port=3306)
>>> Mycursor=Myconn.cursor()
>>> Mycursor.execute('SELECT servername FROM ping_status WHERE pingstatus="failed"')

And see which line you type causes the error. From your error message it looks like the user you have specified doesn't have the appropriate permissions to connect to you database.

Which is even easier to discover by not using Python and just trying to connect using the MySQL command line client;

$ mysql -u yyy -h yyy.yyy.yyy.yyy -p yyy

And see what happens.

Regards,
Andy
--
From the desk of Andrew J Todd esq - http://www.halfcooked.com/
_______________________________________________
DB-SIG maillist  -  DB-SIG@python.org
http://mail.python.org/mailman/listinfo/db-sig

Reply via email to