-1

I have been trying to insert data (a 'hello world' string) from PLC into MongoDB using Python API (which pulls the data from PLC and pushes it into MongoDB). I have been getting the error message:

"D:\Python27\lib\socket.py, line 222, in meth return getattr(self._sock,name)(*args) error: [Errno 10061] No connection could be made because the target machine actively refused it"

despite having mongod running in the Services background for the code I have written below. Also, the server IP address on which MongoDB is present is 10.52.124.186 and address of PLC (which I am using it on my PC) is 10.52.124.135. I am have tried almost everything to sort it out and yet I haven't got a clue as to how to get past it. Where am I going wrong?

#!/usr/bin/python          

import socket
import socket
import pymongo
from pymongo import MongoClient
import datetime

# Connection to server (PLC) on port 27017
server = socket.socket()         
host = '10.52.124.135' 
port = 27017

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

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.AAAA.insert(mongodoc)
finally:
    conn.close() 
Arjun
  • 9
  • 1
  • 7

1 Answers1

0

I don't know much about Python, but it seems odd to me that your closing the socket "server.close" and then trying to use it afterwards "data = server.recv(BUFFER_SIZE)". Anyway, maybe you should start by printing the text from your PLC on the screen instead of trying to do it all in one go. It's usually easier if you break it down into smaller sections and then glue them back together when you know they are working separately.

Also see this post for tracing your error

Community
  • 1
  • 1
nettogrisen
  • 115
  • 2
  • 12