Unable to insert data into MongoDB.

Arjun Srivatsa arjuns123 at gmail.com
Thu Feb 11 10:12:06 EST 2016


Hi guys. I am basically transferring the data from PLC to PC (where the Python API runs) but I'm unable to insert into MongoDB thereafter. When I run the Python script on IDLE, the output is

Hello World!
Traceback (most recent call last): File "C:\Users\SRA2LO\Desktop\API.py", line 32, in <module> s.bind((IP, PORT)) File "C:\Python27\lib\socket.py", line 228, in meth return getattr(self._sock,name)(*args) error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
and when I change the IP of MongoDB server, it shows


Hello World!
Traceback (most recent call last): File "C:\Users\SRA2LO\Desktop\API.py", line 32, in <module> s.bind((IP, PORT)) File "C:\Python27\lib\socket.py", line 228, in meth return getattr(self._sock,name)(*args) error: [Errno 10049] The requested address is not valid in its context.

Could you please help me out? I have disabled the firewall as well.

Here's the API I have written.

#!/usr/bin/python          

import socket
import socket
from pymongo import MongoClient
#from eve import Eve
import datetime

# Connection to server (PLC) on port 27017
server = socket.socket()         
host = "10.52.124.135" #IP of PLC
port = 27017
BUFFER_SIZE = 1024 
###############

server.connect((host, port))
print server.recv(1024)

server.close 

#Connection to Client (Mongodb) on port 27017
IP = "127.0.0.1"
PORT = 27017
BUFFER_SIZE = 1024

client = MongoClient('127.0.0.1', 27017)
db = client.test_database

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
        conn.sendall(data)


        # send to MongoDB

        mongodoc = { "data": data, "date" : datetime.datetime.utcnow() }


        ABC = db.ABC
        ABC_id = ABC.insert_one(mongodoc).inserted_id

finally:
    conn.close()


More information about the Python-list mailing list