使用pymongo插入MongoDB无法提交

时间:2017-02-15 22:40:41

标签: python mongodb

我使用pymongo及以下代码使用MongoDB将一些字段插入到我的文档中

client = pymongo.MongoClient("mongodb://Mahdi-Bazargan-121:P_A_S_S_W_O_D@aws-ap-southeast-1-portal.2.dblayer.com:15312,aws-ap-southeast-1-portal.0.dblayer.com:15312/admin?ssl=true")
      print("client is %s" %client)
      mydb = client['BBCArticles']
      print("db %s" % mydb)
      my_collection = mydb['Articles']
      print("my_collection %s" % my_collection)
      myrecord = {"author": "Duke",
                  "title": "PyMongo 101",
                  "tags": ["MongoDB", "PyMongo", "Tutorial"],
                  "date": datetime.datetime.utcnow()
                  }
      record_id = mydb.mytable.insert(myrecord)
      print("record id %s" %record_id)

然而,我得到了流动的错误

client is MongoClient(host=['aws-ap-southeast-1-portal.0.dblayer.com:15312', 'aws-ap-southeast-1-portal.2.dblayer.com:15312'], document_class=dict, tz_aware=False, connect=True, ssl=
True)
db Database(MongoClient(host=['aws-ap-southeast-1-portal.0.dblayer.com:15312', 'aws-ap-southeast-1-portal.2.dblayer.com:15312'], document_class=dict, tz_aware=False, connect=True, ss
l=True), u'BBCArticles')
my_collection Collection(Database(MongoClient(host=['aws-ap-southeast-1-portal.0.dblayer.com:15312', 'aws-ap-southeast-1-portal.2.dblayer.com:15312'], document_class=dict, tz_aware=F
alse, connect=True, ssl=True), u'BBCArticles'), u'Articles')
2017-02-16 01:40:05 [scrapy.core.scraper] ERROR: Spider error processing <GET http://www.bbc.com/> (referer: http://www.bbc.com/)
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\scrapy-1.3.1-py2.7.egg\scrapy\utils\defer.py", line 102, in iter_errback
    yield next(it)
  File "c:\python27\lib\site-packages\scrapy-1.3.1-py2.7.egg\scrapy\spidermiddlewares\offsite.py", line 29, in process_spider_output
    for x in result:
  File "c:\python27\lib\site-packages\scrapy-1.3.1-py2.7.egg\scrapy\spidermiddlewares\referer.py", line 22, in <genexpr>
    return (_set_referer(r) for r in result or ())
  File "c:\python27\lib\site-packages\scrapy-1.3.1-py2.7.egg\scrapy\spidermiddlewares\urllength.py", line 37, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "c:\python27\lib\site-packages\scrapy-1.3.1-py2.7.egg\scrapy\spidermiddlewares\depth.py", line 58, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "C:\Users\Mehdi\PycharmProjects\WebCrawler\src\Crawler.py", line 97, in parse
    record_id = mydb.mytable.insert(myrecord)
  File "c:\python27\lib\site-packages\pymongo\collection.py", line 2467, in insert
    with self._socket_for_writes() as sock_info:
  File "c:\python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "c:\python27\lib\site-packages\pymongo\mongo_client.py", line 823, in _get_socket
    server = self._get_topology().select_server(selector)
  File "c:\python27\lib\site-packages\pymongo\topology.py", line 214, in select_server
    address))
  File "c:\python27\lib\site-packages\pymongo\topology.py", line 189, in select_servers
    self._error_message(selector))
ServerSelectionTimeoutError: SSL handshake failed: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661),SSL handshake failed: [SSL: CERTIFICATE_VERIFY_FAILED] cert
ificate verify failed (_ssl.c:661)

我正在使用正确的连接字符串,我也在使用Python 2.7

1 个答案:

答案 0 :(得分:1)

改为使用此行

client = pymongo.MongoClient("mongodb://****:****@aws-ap-southeast-1-portal.2.dblayer.com:15312,aws-ap-southeast-1-portal.0.dblayer.com:15312/BBCArticles?ssl=true",
                                   ssl_cert_reqs=ssl.CERT_NONE)