From watson at hf.webex.com Tue Jan 18 03:20:56 2011 From: watson at hf.webex.com (Watson Xu) Date: Tue, 18 Jan 2011 10:20:56 +0800 Subject: [DB-SIG] Cannot connect to Mysql DB when I try to start a Pyhon script from PHP page. Message-ID: <857E42AF7776FF47958F4CEF8CD705E7030D92CF@EXSVR01.hfwbx.hf.webex.com> 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: ---------------------- 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: From andy47 at halfcooked.com Tue Jan 18 12:14:06 2011 From: andy47 at halfcooked.com (Andy Todd) Date: Tue, 18 Jan 2011 22:14:06 +1100 Subject: [DB-SIG] Cannot connect to Mysql DB when I try to start a Python script from PHP page. In-Reply-To: <857E42AF7776FF47958F4CEF8CD705E7030D92CF@EXSVR01.hfwbx.hf.webex.com> References: <857E42AF7776FF47958F4CEF8CD705E7030D92CF@EXSVR01.hfwbx.hf.webex.com> Message-ID: <4D3575FE.7050809@halfcooked.com> 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='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 > > > > _______________________________________________ > DB-SIG maillist - DB-SIG at 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/ From dieter at handshake.de Wed Jan 19 07:47:58 2011 From: dieter at handshake.de (Dieter Maurer) Date: Wed, 19 Jan 2011 07:47:58 +0100 Subject: [DB-SIG] Cannot connect to Mysql DB when I try to start a Pyhon script from PHP page. In-Reply-To: <857E42AF7776FF47958F4CEF8CD705E7030D92CF@EXSVR01.hfwbx.hf.webex.com> References: <857E42AF7776FF47958F4CEF8CD705E7030D92CF@EXSVR01.hfwbx.hf.webex.com> Message-ID: <19766.35102.534081.312371@gargle.gargle.HOWL> Watson Xu wrote at 2011-1-18 10:20 +0800: >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! > ... >Python Script (/var/www/html/test.py): >-------------------- > ... >import MySQLdb > >MySQLdbHost="yyy.yyy.yyy.yyy" >MySQLdbUser="yyy" >MySQLdbPassword="yyy" >MydbSID="yyy" >MydbPort=3306 > ... >Myconn=MySQLdb.connect(host=MySQLdbHost, user=MySQLdbUser, >passwd=MySQLdbPassword, db=MydbSID, port=MydbPort) > ... > super(Connection, self).__init__(*args, **kwargs2) > >_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL >server on '10.224.71.99' (13)") Your problem is neither on the PHP nor the Python side. Instead, it lies with the MySQL connection parameters. Apparently, there is no MySQL at port "3306" at host "10.224.71.99" or it refuses to speak with this client. -- Dieter From dieter at handshake.de Wed Jan 19 08:47:28 2011 From: dieter at handshake.de (Dieter Maurer) Date: Wed, 19 Jan 2011 08:47:28 +0100 Subject: [DB-SIG] Cannot connect to Mysql DB when I try to start a Pyhonscript from PHP page. In-Reply-To: <857E42AF7776FF47958F4CEF8CD705E7030D9571@EXSVR01.hfwbx.hf.webex.com> References: <857E42AF7776FF47958F4CEF8CD705E7030D92CF@EXSVR01.hfwbx.hf.webex.com> <19766.35102.534081.312371@gargle.gargle.HOWL> <857E42AF7776FF47958F4CEF8CD705E7030D9571@EXSVR01.hfwbx.hf.webex.com> Message-ID: <19766.38672.858692.748086@gargle.gargle.HOWL> Watson Xu wrote at 2011-1-19 15:13 +0800: >Hi Dieter, > >I added the port in code, the issue still there. BTW, this script works >fine when I run it manually in Linux server. > >And it is strange that when I change "10.224.71.99" into "localhost". >The issue is gone when I use the PHP page. This, by itself, would indicate a configuration issue at the MySQL server side: the server is listening at "localhost" (i.e. "127.0.0.1") but not at "10.224.71.99". Of course, this does not fit well with the other observation: that the script works when you run the script manually. Often, security problems are disguised (e.g. an authorization failure could be masked as an "OperationalError"). I do not know whether MySQL behaves like this. If it does, your observations might be explained as follows: The MySQL server has loose access restrictions for "localhost" (all "localhost" users are trusted) but strict ones for "public" interfaces. That could explain the second observation. The MySQL server has loose access restrictions for local "normal" users -- but strict ones for your user under which your internet access operates. To verify these assumptions, you could try to login as the user that runs your webserver and as this user run the script (outside PHP). Does the script still work? -- Dieter From watson at hf.webex.com Wed Jan 19 08:13:45 2011 From: watson at hf.webex.com (Watson Xu) Date: Wed, 19 Jan 2011 15:13:45 +0800 Subject: [DB-SIG] Cannot connect to Mysql DB when I try to start a Pyhonscript from PHP page. In-Reply-To: <19766.35102.534081.312371@gargle.gargle.HOWL> References: <857E42AF7776FF47958F4CEF8CD705E7030D92CF@EXSVR01.hfwbx.hf.webex.com> <19766.35102.534081.312371@gargle.gargle.HOWL> Message-ID: <857E42AF7776FF47958F4CEF8CD705E7030D9571@EXSVR01.hfwbx.hf.webex.com> Hi Dieter, I added the port in code, the issue still there. BTW, this script works fine when I run it manually in Linux server. And it is strange that when I change "10.224.71.99" into "localhost". The issue is gone when I use the PHP page. Thanks, Watson -----Original Message----- From: Dieter Maurer [mailto:dieter at handshake.de] Sent: Wednesday, January 19, 2011 2:48 PM To: Watson Xu Cc: db-sig at python.org Subject: Re: [DB-SIG] Cannot connect to Mysql DB when I try to start a Pyhonscript from PHP page. Watson Xu wrote at 2011-1-18 10:20 +0800: >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! > ... >Python Script (/var/www/html/test.py): >-------------------- > ... >import MySQLdb > >MySQLdbHost="yyy.yyy.yyy.yyy" >MySQLdbUser="yyy" >MySQLdbPassword="yyy" >MydbSID="yyy" >MydbPort=3306 > ... >Myconn=MySQLdb.connect(host=MySQLdbHost, user=MySQLdbUser, >passwd=MySQLdbPassword, db=MydbSID, port=MydbPort) > ... > super(Connection, self).__init__(*args, **kwargs2) > >_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL >server on '10.224.71.99' (13)") Your problem is neither on the PHP nor the Python side. Instead, it lies with the MySQL connection parameters. Apparently, there is no MySQL at port "3306" at host "10.224.71.99" or it refuses to speak with this client. -- Dieter