Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Unable to insert data into MongoDB. Date: Mon, 15 Feb 2016 17:33:40 +0100 Organization: None Lines: 67 Message-ID: References: <8bb40e3f-57ce-4d5a-a426-6b56cebc8de7@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de nHxD6yRLvZXl+gmBPKstywE4snMCY2JBSqkprwYD3Dnw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'sys': 0.05; 'inserts': 0.07; 'socket': 0.07; 'scripts': 0.09; 'port))': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'output': 0.13; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'skip:v 30': 0.20; 'fairly': 0.22; 'import': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'host': 0.28; 'values': 0.28; 'actual': 0.28; 'record': 0.29; 'code:': 0.29; 'connection': 0.30; 'too.': 0.30; 'code': 0.30; 'skip:s 30': 0.31; 'third': 0.33; 'combination': 0.33; 'skip:d 20': 0.34; 'server': 0.34; 'so,': 0.35; 'reply.': 0.35; 'skip:m 40': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'client': 0.37; 'received:org': 0.37; 'desired': 0.37; 'thank': 0.38; 'mean': 0.38; 'data': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'sample': 0.63 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8092.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102971 Arjun Srivatsa wrote: > Hi Peter. > > Thank you for the reply. > > This is the read_server code: > > import socket > from pymongo import MongoClient > #import datetime > import sys > > # Connection to server (PLC) on port 27017 > host = "10.52.124.135" > port = 27017 > > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > s.connect((host, port)) > sys.stdout.write(s.recv(1024)) > > > And the write_db code: > > from pymongo import MongoClient > import datetime > import socket > import sys > > client = MongoClient('mongodb://localhost:27017/') > db = client.test_database > > mongodoc = { "data": 'data', "date" : datetime.datetime.utcnow() } > values = db.values > values_id = values.insert_one(mongodoc).inserted_id > > > So, both these scripts work independently. While, read_server shows the > output of the actual data from PLC, write_db inserts the sample data into > the MongoDB. > > I am not sure as to how to combine these both and get the desired output. What I mean is once you have working scripts connect_to_mongodb() while True: record = make_fake_data() insert_record_into_mongodb(record) and connect_to_server() while True: record = read_record_from_server() print(record) you can combine the code in a third script to connect_to_server() connect_to_mongodb() while True: record = read_record_from_server() insert_record_into_mongodb(record) and be fairly sure that the combination works, too.