1

这可以从命令行正常工作,但不能通过网络:

#!/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 在本地主机上运行。

4

1 回答 1

0

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

尝试(作为根):

setsebool httpd_can_network_connect 
setsebool httpd_can_network_connect_db 1
于 2012-11-29T15:51:56.500 回答