Can't connect to MongoDB with cgi script - python

This works fine from the command line, but not through the web:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cgitb
cgitb.enable()
from pymongo import Connection
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "<HTML>Here</HTML>"
connection = Connection()
The file is in my cgi-bin directory. When I try to run it through the web, I get:
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/var/www/cgi-bin/test2.py
9 print # blank line, end of headers
10 print "<TITLE>CGI script output</TITLE>"
11 print "<HTML>Here</HTML>"
12
13 connection = Connection()
connection undefined, Connection = <class 'pymongo.connection.Connection'>
/usr/lib64/python2.4/site-packages/pymongo/connection.py in __init__(self=Connection(None, None), host=['localhost'], port=27017, pool_size=None, auto_start_request=None, timeout=None, slave_okay=False, network_timeout=None, document_class=<type 'dict'>, tz_aware=False, _connect=True)
303
304 if _connect:
305 self.__find_master()
306
307 if username:
self = Connection(None, None), self.__find_master = Database(Connection(None, None), u'__find_master')
/usr/lib64/python2.4/site-packages/pymongo/connection.py in __find_master(self=Connection(None, None))
507 return node
508
509 raise AutoReconnect("could not find master/primary")
510
511 def __connect(self):
global AutoReconnect = <class pymongo.errors.AutoReconnect>
AutoReconnect: could not find master/primary
args = ('could not find master/primary',)
MongoDB is running on localhost.

I have resolved this myself. My script's access to the DB was being blocked by SELinux.
Try (as root):
setsebool httpd_can_network_connect
setsebool httpd_can_network_connect_db 1

Related

Twisted lineReceived not getting called

I encountered a strange behavior when i was building a command line interface in python. Here is the striped down version of the code that can reproduce the issue.
from twisted.internet import reactor, stdio
from twisted.protocols import basic
class CommandLine(basic.LineReceiver):
def __init__(self):
self.linebuf = ''
self.setLineMode()
# why lineReceived doesn't work?
# def lineReceived(self, data):
def dataReceived(self, data):
print 'data received ' + ' '.join([str(ord(c)) for c in data ])
print data
if __name__=='__main__':
stdio.StandardIO(CommandLine())
reactor.run()
The code above works as intended, out put in the form of "data received 108 115 115 10" is printed everytime a line is entered. Here is a sample output using dataReceived:
$ python cmdline.py
hello
data received 104 101 108 108 111 10
hello
^[[A
data received 27 91 65 10
However nothing gets printed out except the echo of the command line itself when I use lineReceived instead of dataReceived in the code above. Example output using lineReceived:
$ python cmdline.py
hello
^[[A
According the the documentation on lineReceived, the lineReceived function gets invoked when a line is received with the LineReceiver in line mode.
For now I am using dataReceived to make it work. But I would like to find out why lineReceived is not working as intended. Any hint, suggestions, advice would be very much appreciated!
Regards.
The reason is line delimiter constant which is set to r'\r\n' by default (MS Windows delimiter). Try to set it to '\n' (Linux and Mac OS) instead:
class CommandLine(basic.LineReceiver):
delimiter = '\n'

import nltk error in xampp, or on wamp

I am using nltk (installed and works fine in IDLE) for a project in Python (2.7.4) which runs perfectly fine on IDLE, but using the same code in xampp or wamp (cgi-bin), everything related to 'nltk' doesn't works and this is the error shown by adding these lines
import cgitb
cgitb.enable()
The errors are in lines are marked by '=>' and details are enclosed within ** and **. I've tried printing 'sys.path' after importing os, which shows the 'site-packages' directory in which 'nltk' resides. I even copied and pasted this folder into 'C:\Python27\' but still the same errors. Things other than nltk works as desired.
<type 'exceptions.ValueError'> Python 2.7.4: C:\python27\python.exe
Wed May 14 16:13:34 2014
A problem occurred in a Python script. Here is the sequence of function calls
leading up to the error, in the order they occurred.
C:\xampp\cgi-bin\Major\project.py in ()
=> 73 import nltk
**nltk undefined**
C:\python27\nltk\__init__.py in ()
159 import cluster; from cluster import *
160
=> 161 from downloader import download, download_shell
162 try:
163 import Tkinter
**downloader undefined, download undefined, download_shell undefined**
C:\python27\nltk\downloader.py in ()
2199
2200 # Aliases
=> 2201 _downloader = Downloader()
2202 download = _downloader.download
2203 def download_shell(): DownloaderShell(_downloader).run()
**_downloader undefined, Downloader = None**
C:\python27\nltk\downloader.py in __init__(self=<nltk.downloader.Downl
oader object>, server_index_url=None, download_dir=None)
425 # decide where we're going to save things to.
426 if self._download_dir is None:
=> 427 self._download_dir = self.default_download_dir()
428
429 #/////////////////////////////////////////////////////////////////
**self = <nltk.downloader.Downloader object>, self._download_dir = None,
self.default_download_dir = <bound method Downloader.default_download_dir of
<nltk.downloader.Downloader object>>**
C:\python27\nltk\downloader.py in default_download_dir(self=<nltk.downloa
der.Downloader object>)
926 homedir = os.path.expanduser('~/')
927 if homedir == '~/':
=> 928 raise ValueError("Could not find a default
download directory")
929
930 # append "nltk_data" to the home directory
**<pre>builtin ValueError = < type 'exceptions.ValueError' > <pre/>**
<type 'exceptions.ValueError'>: Could not find a default download directory
args = ('Could not find a default download directory',)
message = 'Could not find a default download directory'

accessing a database from a python cgi

I configured the httpd file to run python scripts as given in a website. After the configuration I was amazed to see .py file getting executed when placed in the htdocs folder, but .cgi files are not being executed. The error says an internal error.
Thought of proceeding with .py files but when I try to access mysql database, I am not able to.
My .py file is:
import cgi
import MySQLdb
print "Content-type: text/html"
print
print "<html><head>"
print ""
print "</head><body>"
form=cgi.FieldStorage()
name=form["t1"].value
print "Hello. %s" %name
print "hai"
print '<input type="submit"/>'
Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="root", passwd="pwd", db="db1")
cursor = Con.cursor()
sql="SELECT * FROM rec"
cursor.execute(sql)
data = cursor.fetchone()
print "%s" %data
print "</body></html>"
I am not getting any error, but 'data' is not getting printed
output I got was:
hello name hai submit button
new to python. So can u guys please help me out?
python version-2.7
db-mysql
server-apache 2.2
win32 bit
Put following line at first line, and see what error happened (traceback).
import cgitb; cgitb.enable()

Read Apache access log file from a python CGI script and extract ip addresses real time

I'm trying to read Apache access log file from a python script and extract ip addresses real time. I'm using python CGI programming.
I have given 777 permissions.
-rwxrwxr-x 1 root adm 1012822 Jun 5 13:02 /var/log/apache2/access.log
This is the code I'm using
for line in open('/var/log/apache2/access.log'):
ip = line.split(' ')[0]
print ip
But following Error is displayed. What is the problem here?
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/usr/lib/cgi-bin/log.py in ()
6 form = cgi.FieldStorage()
7
=> 8 for line in open('/var/log/apache2/access.log'):
9 ip = line.split(' ')[0]
10 print ip
line undefined, builtin open = <built-in function open>
<type 'exceptions.IOError'>: [Errno 13] Permission denied: '/var/log/apache2/access.log'
args = (13, 'Permission denied')
errno = 13
filename = '/var/log/apache2/access.log'
message = ''
strerror = 'Permission denied'

Talking to supervisord over xmlrpc

I'm trying to talk to supervisor over xmlrpc. Based on supervisorctl (especially this line), I have the following, which seems like it should work, and indeed it works, in so far as it connects enough to receive an error from the server:
#socketpath is the full path to the socket, which exists
# None and None are the default username and password in the supervisorctl options
In [12]: proxy = xmlrpclib.ServerProxy('http://127.0.0.1', transport=supervisor.xmlrpc.SupervisorTransport(None, None, serverurl='unix://'+socketpath))
In [13]: proxy.supervisor.getState()
Resulting in this error:
---------------------------------------------------------------------------
ProtocolError Traceback (most recent call last)
/home/marcintustin/webapps/django/oneclickcosvirt/oneclickcos/<ipython-input-13-646258924bc2> in <module>()
----> 1 proxy.supervisor.getState()
/usr/local/lib/python2.7/xmlrpclib.pyc in __call__(self, *args)
1222 return _Method(self.__send, "%s.%s" % (self.__name, name))
1223 def __call__(self, *args):
-> 1224 return self.__send(self.__name, args)
1225
1226 ##
/usr/local/lib/python2.7/xmlrpclib.pyc in __request(self, methodname, params)
1576 self.__handler,
1577 request,
-> 1578 verbose=self.__verbose
1579 )
1580
/home/marcintustin/webapps/django/oneclickcosvirt/lib/python2.7/site-packages/supervisor/xmlrpc.pyc in request(self, host, handler, request_body, verbose)
469 r.status,
470 r.reason,
--> 471 '' )
472 data = r.read()
473 p, u = self.getparser()
ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 401 Unauthorized>
This is the unix_http_server section of supervisord.conf:
[unix_http_server]
file=/home/marcintustin/webapps/django/oneclickcosvirt/tmp/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
So, there should be no authentication problems.
It seems like my code is in all material respects identical to the equivalent code from supervisorctl, but supervisorctl actually works. What am I doing wrong?
Your code looks substantially correct. I'm running Supervisor 3.0 with Python 2.7, and given the following:
import supervisor.xmlrpc
import xmlrpclib
p = xmlrpclib.ServerProxy('http://127.0.0.1',
transport=supervisor.xmlrpc.SupervisorTransport(
None, None,
'unix:///home/lars/lib/supervisor/tmp/supervisor.sock'))
print p.supervisor.getState()
I get:
{'statename': 'RUNNING', 'statecode': 1}
Are you certain that your running Supervisor instance is using the configuration file you think it is? What if you run supervisord in debug mode, do you see the connection?
I don't use the ServerProxy from xmlrpclib, I use the Server class instead and I don't have to define any transports or paths to sockets. Not sure if your purposes require that, but here's a thin client I use fairly frequently. It's pretty much straight out of the docs.
python -c "import xmlrpclib;\
supervisor_client = xmlrpclib.Server('http://localhost:9001/RPC2');\
print( supervisor_client.supervisor.stopProcess(<some_proc_name>) )"
I faced the same issue; the problem was simple; supervisord was not running!
First:
supervisord
And then:
supervisorctl start all
Done! :)
If you've set nodaemon to true, you must keep the process runing in another tab of your terminal.

Categories