title: Unable to insert data from PLC into MongoDB using Pymongo
body: <p>I want to insert data(a string - <code>'Hello World'</code>) from PLC into MongoDB using Pymongo. I cannot figure out why a collection isn't being created and <code>'Hello World'</code> is not inserted into it. Could you please explain as to where I am going wrong? Here's the code I have written so far:</p>
  
  <pre><code>    #!/usr/bin/python
      import socket         
      import socket
      from pymongo import MongoClient
      import datetime
  
      # Connection to server (PLC) PLC's IP addr is .135
      server = socket.socket()         
      host = '10.52.124.135' 
      port = 4840  
  
      server.connect((host, port))
      print server.recv(1024)
  
      server.close 
  
      #Connection to Client (Mongodb) on port 27017
      IP = '10.52.124.186'
      PORT = 27017
      BUFFER_SIZE = 1024
  
      client = MongoClient('10.52.124.186', 27017)
      db = client.RXMMongoDB
      collection = db.ABC
  
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind((IP, PORT))
      s.listen(1)
  
      # connections loop
      while True:
              conn, addr = s.accept()
              print 'Connection address:',addr
              try:
                       # read loop
                       while True:
                            data = server.recv(BUFFER_SIZE)
  
                            if not data: 
                                    break
  
                            # send to Mongo
                            mongodoc = { "data": data, "date" : datetime.datetime.utcnow() }
                            db.ABC.insert_one(mongodoc)
              finally:
                       conn.close() 
  </code></pre>