Quantcast

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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Watson Xu

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='[hidden email]'

mail_me="[hidden email]"

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  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot connect to Mysql DB when I try to start a Python script from PHP page.

Andy Todd
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='[hidden email]'
>
> mail_me="[hidden email]"
>
> 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  -  [hidden email]
> 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  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot connect to Mysql DB when I try to start a Pyhon script from PHP page.

Dieter Maurer
In reply to this post by Watson Xu
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
_______________________________________________
DB-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot connect to Mysql DB when I try to start a Pyhonscript from PHP page.

Watson Xu
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:[hidden email]]
Sent: Wednesday, January 19, 2011 2:48 PM
To: Watson Xu
Cc: [hidden email]
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
_______________________________________________
DB-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot connect to Mysql DB when I try to start a Pyhonscript from PHP page.

Dieter Maurer
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
_______________________________________________
DB-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig
Loading...