Unable to insert data into MongoDB.

Arjun Srivatsa arjuns123 at gmail.com
Mon Feb 15 10:00:22 EST 2016

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 = "" 
port = 27017

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

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. 

On Monday, February 15, 2016 at 12:37:02 PM UTC+1, Peter Otten wrote:
> Arjun Srivatsa wrote:
> > I changed the port number from 27017 to 55555 in the code segment:
> Instead of throwing arbitrary changes at your script in the hope that one 
> works I recommend that you write two independent scripts, from scratch:
> (1) write_to_db.py: 
> Write made-up data into the MongoDB on your local machine.
> (2) read_from_server.py: 
> Read data from the server and print it to stdout.
> Reduce these scripts to the bare minimum. Debug them one ofter another.
> Once both scripts work to your satisfaction replace the sample data in 
> script one with code from script two that reads actual data.

More information about the Python-list mailing list