折腾:
期间,
虽然timeout了,但是的确是可以运行的:
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# python app.py
[2018-04-24 09:13:52,620 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7f40da1a1da0>
[2018-04-24 09:13:52,622 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-24 09:13:52,987 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7fa0544f9e10>
[2018-04-24 09:13:52,989 INFO app.py:80 <module>] purePymongo=MongoClient(host=[‘x.x.x.x:port’], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
WARNING:werkzeug: * Debugger is active!
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:104]:
parser=<flask_restful.reqparse.RequestParser object at 0x7fa050036860>
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:14:06,064 INFO app.py:104 get] parser=<flask_restful.reqparse.RequestParser object at 0x7fa050036860>
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:107]:
parsedArgs={‘q’: ‘play a happy song’}
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:14:06,066 INFO app.py:107 get] parsedArgs={‘q’: ‘play a happy song’}
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:113]:
q=play a happy song
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:14:06,067 INFO app.py:113 get] q=play a happy song
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:122]:
foundSongName=<_sre.SRE_Match object; span=(0, 17), match=’play a happy song’>
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:14:06,067 INFO app.py:122 get] foundSongName=<_sre.SRE_Match object; span=(0, 17), match=’play a happy song’>
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:128]:
songName=happy
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:14:06,068 INFO app.py:128 get] songName=happy
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:150]:
filenameRegex=re.compile(‘happy’, re.IGNORECASE)
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:14:06,068 INFO app.py:150 get] filenameRegex=re.compile(‘happy’, re.IGNORECASE)
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:156]:
findFileCursor=<gridfs.grid_file.GridOutCursor object at 0x7fa050036a58>
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:14:06,068 INFO app.py:156 get] findFileCursor=<gridfs.grid_file.GridOutCursor object at 0x7fa050036a58>
Traceback (most recent call last):
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask_restful/__init__.py", line 273, in error_router
return original_handler(e)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask_restful/__init__.py", line 273, in error_router
return original_handler(e)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask_restful/__init__.py", line 480, in wrapper
resp = resource(*args, **kwargs)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/flask_restful/__init__.py", line 595, in dispatch_request
resp = meth(*args, **kwargs)
File "/root/naturling_20180101/web/server/robotDemo/app.py", line 157, in get
findFileCount = findFileCursor.count()
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/pymongo/cursor.py", line 755, in count
cmd, self.__collation, session=self.__session)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/pymongo/collection.py", line 1504, in _count
with self._socket_for_reads() as (sock_info, slave_ok):
File "/usr/lib64/python3.4/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/pymongo/mongo_client.py", line 982, in _socket_for_reads
server = topology.select_server(read_preference)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/pymongo/topology.py", line 224, in select_server
address))
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/pymongo/topology.py", line 183, in select_servers
selector, server_timeout, address)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.4/site-packages/pymongo/topology.py", line 199, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: x.x.x.x:32018: timed out
去用postman测试,也是一样现象:
对于Flask中代码:
log.info("findFileCursor=%s", findFileCursor)
findFileCount = findFileCursor.count()
log.info("findFileCount=%s", findFileCount)
卡死在findFileCursor.count()了:
flask pymongo.errors.ServerSelectionTimeoutError timed out
python – Why is PyMongo 3 giving ServerSelectionTimeoutError? – Stack Overflow
python – Pymongo error Server Timeout – Stack Overflow
pymongo.errors.ServerSelectionTimeoutError · Issue #1734 · MongoEngine/mongoengine
errors – Exceptions raised by the pymongo package — PyMongo 3.6.1 documentation
pymongo.errors.ServerSelectionTimeoutError Python Example
[PYTHON-1054] Database connection timeout error – MongoDB
pymongo count() pymongo.errors.ServerSelectionTimeoutError
python – Error while counting number of entries in a Mongo database using pymongo – Stack Overflow
python – How do you check if the client for a MongoDB instance is valid? – Stack Overflow
pymongo.MongoClient Python Example
好像此处不是count()出错,而是本身数据库连接有问题
突然想到,难道是:
此处阿里云的端口限制,不允许其他IP包括自己的服务器本身去访问自己的mongo的端口?
此处确认服务器上的mongod是正在正常运行的:
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# systemctl status mongod
● mongod.service – SYSV: Mongo is a scalable, document-oriented database.
Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled)
Active: active (running) since Tue 2018-04-10 15:37:29 CST; 1 weeks 6 days ago
Docs: man:systemd-sysv-generator(8)
CGroup: /system.slice/mongod.service
└─1096 /usr/bin/mongod -f /etc/mongod.conf
Apr 10 15:37:28 naturling-general-01 systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database….
Apr 10 15:37:28 naturling-general-01 runuser[1077]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
Apr 10 15:37:29 naturling-general-01 runuser[1077]: pam_unix(runuser:session): session closed for user mongod
Apr 10 15:37:29 naturling-general-01 mongod[1058]: Starting mongod: [ OK ]
Apr 10 15:37:29 naturling-general-01 systemd[1]: Started SYSV: Mongo is a scalable, document-oriented database..
把:
purePymongo = MongoClient(
host="x.x.x.x",
改为
purePymongo = MongoClient(
host=“localhost”,
同时加上:
mongoServerInfo = purePymongo.server_info()
log.info("mongoServerInfo=%s", mongoServerInfo)
看看效果
是可以打印出mongo的信息的:
(robotDemo-dwdcgdaG) [root@naturling-general-01 robotDemo]# python app.py
[2018-04-24 09:42:51,418 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7fb77c948da0>
[2018-04-24 09:42:51,420 INFO app.py:81 <module>] purePymongo=MongoClient(host=[‘localhost:32018′], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-24 09:42:51,630 INFO app.py:83 <module>] mongoServerInfo={‘maxBsonObjectSize’: 16777216, ‘modules’: [], ‘sysInfo’: ‘deprecated’, ‘buildEnvironment’: {‘cxx’: ‘/opt/mongodbtoolchain/bin/g++: g++ (GCC) 4.8.2’, ‘linkflags’: ‘-fPIC -pthread -Wl,-z,now -rdynamic -fuse-ld=gold -Wl,-z,noexecstack -Wl,–warn-execstack’, ‘distarch’: ‘x86_64’, ‘ccflags’: ‘-fno-omit-frame-pointer -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -O2 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-missing-braces -fno-builtin-memcmp’, ‘target_arch’: ‘x86_64’, ‘target_os’: ‘linux’, ‘distmod’: ‘rhel70’, ‘cc’: ‘/opt/mongodbtoolchain/bin/gcc: gcc (GCC) 4.8.2’, ‘cxxflags’: ‘-Wnon-virtual-dtor -Woverloaded-virtual -Wno-maybe-uninitialized -std=c++11’}, ‘allocator’: ‘tcmalloc’, ‘ok’: 1.0, ‘bits’: 64, ‘gitVersion’: ‘a9f574de6a566a58b24d126b44a56718d181e989’, ‘versionArray’: [3, 2, 19, 0], ‘openssl’: {‘running’: ‘OpenSSL 1.0.1e-fips 11 Feb 2013’, ‘compiled’: ‘OpenSSL 1.0.1e-fips 11 Feb 2013’}, ‘debug’: False, ‘storageEngines’: [‘devnull’, ‘ephemeralForTest’, ‘mmapv1’, ‘wiredTiger’], ‘version’: ‘3.2.19’, ‘javascriptEngine’: ‘mozjs’}
[2018-04-24 09:42:51,992 INFO app.py:59 <module>] api=<flask_restful.Api object at 0x7fe841096e10>
[2018-04-24 09:42:51,995 INFO app.py:81 <module>] purePymongo=MongoClient(host=[‘localhost:32018′], document_class=dict, tz_aware=False, connect=True, authsource=’gridfs’)
[2018-04-24 09:42:52,005 INFO app.py:83 <module>] mongoServerInfo={‘buildEnvironment’: {‘cxx’: ‘/opt/mongodbtoolchain/bin/g++: g++ (GCC) 4.8.2’, ‘distarch’: ‘x86_64’, ‘linkflags’: ‘-fPIC -pthread -Wl,-z,now -rdynamic -fuse-ld=gold -Wl,-z,noexecstack -Wl,–warn-execstack’, ‘target_os’: ‘linux’, ‘distmod’: ‘rhel70’, ‘cc’: ‘/opt/mongodbtoolchain/bin/gcc: gcc (GCC) 4.8.2’, ‘cxxflags’: ‘-Wnon-virtual-dtor -Woverloaded-virtual -Wno-maybe-uninitialized -std=c++11’, ‘ccflags’: ‘-fno-omit-frame-pointer -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -O2 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-missing-braces -fno-builtin-memcmp’, ‘target_arch’: ‘x86_64’}, ‘maxBsonObjectSize’: 16777216, ‘gitVersion’: ‘a9f574de6a566a58b24d126b44a56718d181e989’, ‘sysInfo’: ‘deprecated’, ‘javascriptEngine’: ‘mozjs’, ‘versionArray’: [3, 2, 19, 0], ‘openssl’: {‘running’: ‘OpenSSL 1.0.1e-fips 11 Feb 2013’, ‘compiled’: ‘OpenSSL 1.0.1e-fips 11 Feb 2013’}, ‘storageEngines’: [‘devnull’, ‘ephemeralForTest’, ‘mmapv1’, ‘wiredTiger’], ‘allocator’: ‘tcmalloc’, ‘ok’: 1.0, ‘modules’: [], ‘debug’: False, ‘bits’: 64, ‘version’: ‘3.2.19’}
WARNING:werkzeug: * Debugger is active!
然后就可以了:
findFileCursor=<gridfs.grid_file.GridOutCursor object at 0x7fe83c9c8f28>
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:43:55,291 INFO app.py:159 get] findFileCursor=<gridfs.grid_file.GridOutCursor object at 0x7fe83c9c8f28>
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:161]:
findFileCount=5
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:43:55,295 INFO app.py:161 get] findFileCount=5
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:169]:
[ 1] _id=5ad44098a4bc71fa65bb2c2e, contentType=audio/mpeg, filename=Happy Birthday,Danny and the Dinosaur!.mp3
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:43:55,299 INFO app.py:169 get] [ 1] _id=5ad44098a4bc71fa65bb2c2e, contentType=audio/mpeg, filename=Happy Birthday,Danny and the Dinosaur!.mp3
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:169]:
[ 2] _id=5ad44095a4bc71fa65bb2bff, contentType=application/pdf, filename=Happy Birthday,Danny and the Dinosaur!.pdf
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:43:55,299 INFO app.py:169 get] [ 2] _id=5ad44095a4bc71fa65bb2bff, contentType=application/pdf, filename=Happy Birthday,Danny and the Dinosaur!.pdf
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:169]:
[ 3] _id=5ad43ad2a4bc71fa65bae49f, contentType=application/pdf, filename=Happy Easter, Little Critter.pdf
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:43:55,300 INFO app.py:169 get] [ 3] _id=5ad43ad2a4bc71fa65bae49f, contentType=application/pdf, filename=Happy Easter, Little Critter.pdf
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:169]:
[ 4] _id=5ad43cc2a4bc71fa65bafda5, contentType=audio/mpeg, filename=Happy Pig Day.mp3
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:43:55,300 INFO app.py:169 get] [ 4] _id=5ad43cc2a4bc71fa65bafda5, contentType=audio/mpeg, filename=Happy Pig Day.mp3
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
INFO in app [app.py:169]:
[ 5] _id=5ad43a2da4bc71fa65badc0a, contentType=application/pdf, filename=Happy father’s day.pdf
<span style="font-family: Monaco; font-size: 12px; color: rgb(51, 51, 51);"–<——————————————————————————
[2018-04-24 09:43:55,300 INFO app.py:169 get] [ 5] _id=5ad43a2da4bc71fa65badc0a, contentType=application/pdf, filename=Happy father’s day.pdf
【总结】
所以现在现象就是:
Mac本地测试连接远程Mongod时,服务器的host设置的IP地址:
from pymongo import MongoClient
purePymongo = MongoClient(
host="x.x.x.x",
而放到服务器上运行时,地址就要改为localhost:
from pymongo import MongoClient
purePymongo = MongoClient(
host="localhost",
才可以正常访问,否则就会出现:
pymongo.errors.ServerSelectionTimeoutError
的问题。
根本原因,以后再深究。
转载请注明:在路上 » 【已解决】pymongo的count()出错:pymongo.errors.ServerSelectionTimeoutError timed out