[DB-SIG] Cannot connect to Mysql DB when I try to start a Pyhon script from PHP page.

Watson Xu watson at hf.webex.com
Tue Jan 18 03:20:56 CET 2011


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='watson at hf.webex.com'

mail_me="xxxxx at 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/db-sig/attachments/20110118/6b4e2b0c/attachment.html>


More information about the DB-SIG mailing list