Unable to insert data into MongoDB.

Arjun Srivatsa arjuns123 at gmail.com
Mon Feb 15 05:44:34 EST 2016


I changed the port number from 27017 to 55555 in the code segment: 

    IP = "127.0.0.1" 
    PORT = 27017 
    BUFFER_SIZE = 1024 
    client = MongoClient('127.0.0.1', 27017) 

And my output on Python shell is: 

hello world!
Connection address: ('127.0.0.1', 16951)
Connection address: ('127.0.0.1', 16953)
Connection address: ('127.0.0.1', 16957)
Connection address: ('127.0.0.1', 16958)
Connection address: ('127.0.0.1', 16959)
Connection address: ('127.0.0.1', 16961)
Connection address: ('127.0.0.1', 16962)
Connection address: ('127.0.0.1', 16963)
Connection address: ('127.0.0.1', 16964)
Connection address: ('127.0.0.1', 16965)

and it goes on and on...

What does the above port numbers mean? And I can't still see the data being inserted into Database yet. 

And as you said, if I change server.close to server.close(), then I get a traceback:

Traceback (most recent call last):
  File "C:\Users\SRA2LO\Desktop\API.py", line 41, in <module>
    data = server.recv(BUFFER_SIZE)
  File "C:\Python27\lib\socket.py", line 174, in _dummy
    raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor


On Thursday, February 11, 2016 at 5:09:53 PM UTC+1, MRAB wrote:
> On 2016-02-11 15:12, Arjun Srivatsa wrote:
> > 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()
> >
> I don't know whether it's relevant, but you didn't close the server 
> socket. You have "server.close" instead of "server.close()".
> 
> Also, the code as posted won't compile because the block after the 
> "while True:" isn't indented.



More information about the Python-list mailing list