Hi all,

I'm setting up a web server using the standard LAMP stack ( Fedora 14,
Apache 2.2 , MySQL 5.1.52, Python 2.7 ). I've written a Python CGI script
which connects to a MySQL server on other host. From a standalone python
script, I am able to connect to the MySQL server. However, as a CGI script,
the connection fails. Apache's error_log shows the following:

 [error] [client 10.239.70.12] Traceback (most recent call last):
 [error] [client 10.239.70.12]   File "/var/www/cgi-bin/temp.py", line 129,
in <module>
 [error] [client 10.239.70.12]     dataSource.Initialize()
 [error] [client 10.239.70.12]   File "/var/www/cgi-bin/temp.py", line 34,
in Initialize
 [error] [client 10.239.70.12]     self.iConnection    = MySQLdb.connect(
STOR_HOST, STOR_USER, STOR_PASS, STOR_DBSE )
 [error] [client 10.239.70.12]   File
"/usr/lib64/python2.7/site-packages/MySQLdb/__init__.py", line 81, in
Connect
 [error] [client 10.239.70.12]     return Connection(*args, **kwargs)
 [error] [client 10.239.70.12]   File
"/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line 187, in
__init__
 [error] [client 10.239.70.12]     super(Connection, self).__init__(*args,
**kwargs2)
 [error] [client 10.239.70.12] _mysql_exceptions.OperationalError: (2003,
"Can't connect to MySQL server on '10.245.18.74' (13)")

However, if I try to connect to mysql server in localhost instead of
connecting to a different host, the connection succeeds.

Still strange, I've written a simple utility in Python which imitates a HTTP
Server ( attached ). This simple server doesn't complain if a CGI script
inside it tries to connect to a mysql server at different host.

I am not sure whether this is a configuration issue / security issue. Please
advise on how to achieve connection to mysql server at different host within
Apache CGI.

Thanks
Gopi
#!/usr/bin/env python

import os                   # Base package
import sys                  # Low level system IO
import BaseHTTPServer       # Implements a simple web server
import CGIHTTPServer        # Used to run CGI scripts
import commands             # Used for running commands in terminal


class CCGIRequestHandler( CGIHTTPServer.CGIHTTPRequestHandler ):
    pass


################################################################################
# Starts the HTTP server
#
def runServer():
    # Fork and kill the parent
    if os.fork():
        sys.stderr.flush()
        sys.stdout.flush()
        os._exit( 0 )

    # Change the port according to the requirement
    serverPort      = 8001
    serverAddress   = ( '', serverPort )

    httpServer      = BaseHTTPServer.HTTPServer( serverAddress, 
CCGIRequestHandler )
    print '[I] HTTP Server started. Serving at port ', serverPort
    httpServer.serve_forever()

################################################################################
# Kills the running server
#
def killServer():
    retStatus, output = commands.getstatusoutput( 'ps | grep ServerCon' )
    lines             = output.splitlines() # This required as we'd have
    lineOfConcern     = lines[0]            # 2 instances of this crap running
    temp              = lineOfConcern.split( ' ' )
    idToKill          = temp[0]
    killCommand       = 'kill -9 ' + idToKill
    retStatus, output = commands.getstatusoutput( killCommand )
    if ( retStatus == 0 ):
        print '[I]Server committed suicide...'
    else:
        print '[E]Cannot kill server. Manually kill it yourself'

################################################################################
# Prints to console whether the server is running or not
#
def reportStatus():
    retStatus, output = commands.getstatusoutput( 'ps | grep ServerCon' )
    lines             = output.splitlines() # This required as we'd have
    if ( len( lines ) > 2 ):
        print '[I]More than one instance of server running. Check'
    elif ( len( lines ) == 2 ):
        print '[I]Server running'
    else:
        print '[I]Server not running'

################################################################################
# Main entry point
#
if __name__ == "__main__":
    print " HTTP Server Control script"
    print " What do you want to do ?"
    print " [ 1 ] Start Server"
    print " [ 2 ] Shutdown server"
    print " [ 3 ] Check status of server"
    option = raw_input( " Enter your option : " )

    if ( option == '1' ):
        print 'You selected option [1]. Starting server'
        runServer()
    elif ( option == '2' ):
        print 'You selected option [2]. Shutting down server'
        killServer()
    elif ( option == '3' ):
        print 'You selected option [3]. Checking status of server'
        reportStatus()
    else:
        print '[E]Invalid option, try again'

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscr...@httpd.apache.org
   "   from the digest: users-digest-unsubscr...@httpd.apache.org
For additional commands, e-mail: users-h...@httpd.apache.org

Reply via email to