无法使用cgi脚本连接到MongoDB

时间:2012-11-26 20:48:01

标签: python cgi pymongo

这在命令行中运行良好,但不能通过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()

该文件位于我的cgi-bin目录中。当我尝试通过网络运行时,我得到:

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正在localhost上运行。

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题。 SELinux阻止了我的脚本对数据库的访问。

尝试(以root身份):

setsebool httpd_can_network_connect 
setsebool httpd_can_network_connect_db 1