Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102811 > unrolled thread
| Started by | Arjun Srivatsa <arjuns123@gmail.com> |
|---|---|
| First post | 2016-02-11 07:12 -0800 |
| Last post | 2016-02-11 20:06 -0500 |
| Articles | 10 — 4 participants |
Back to article view | Back to comp.lang.python
Unable to insert data into MongoDB. Arjun Srivatsa <arjuns123@gmail.com> - 2016-02-11 07:12 -0800
Re: Unable to insert data into MongoDB. Arjun Srivatsa <arjuns123@gmail.com> - 2016-02-11 08:07 -0800
Re: Unable to insert data into MongoDB. MRAB <python@mrabarnett.plus.com> - 2016-02-11 16:09 +0000
Re: Unable to insert data into MongoDB. Arjun Srivatsa <arjuns123@gmail.com> - 2016-02-15 02:44 -0800
Re: Unable to insert data into MongoDB. Peter Otten <__peter__@web.de> - 2016-02-15 12:36 +0100
Re: Unable to insert data into MongoDB. Arjun Srivatsa <arjuns123@gmail.com> - 2016-02-15 07:00 -0800
Re: Unable to insert data into MongoDB. Arjun Srivatsa <arjuns123@gmail.com> - 2016-02-15 07:28 -0800
Re: Unable to insert data into MongoDB. Peter Otten <__peter__@web.de> - 2016-02-15 17:33 +0100
Re: Unable to insert data into MongoDB. Arjun Srivatsa <arjuns123@gmail.com> - 2016-02-16 04:41 -0800
Re: Unable to insert data into MongoDB. Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-02-11 20:06 -0500
| From | Arjun Srivatsa <arjuns123@gmail.com> |
|---|---|
| Date | 2016-02-11 07:12 -0800 |
| Subject | Unable to insert data into MongoDB. |
| Message-ID | <eebb44e3-fcd3-4503-ad2b-5cfcc75ef6fa@googlegroups.com> |
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()
[toc] | [next] | [standalone]
| From | Arjun Srivatsa <arjuns123@gmail.com> |
|---|---|
| Date | 2016-02-11 08:07 -0800 |
| Message-ID | <8265726c-a5af-4110-8dcd-33dccd5aecb3@googlegroups.com> |
| In reply to | #102811 |
Hello,
I changed
PORT = 27017
to
PORT = 55555.
I am not getting the error anymore. But I am still unable to insert the data into MongoDB.
On Thursday, February 11, 2016 at 4:12:30 PM UTC+1, 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()
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-02-11 16:09 +0000 |
| Message-ID | <mailman.50.1455206980.22075.python-list@python.org> |
| In reply to | #102811 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Arjun Srivatsa <arjuns123@gmail.com> |
|---|---|
| Date | 2016-02-15 02:44 -0800 |
| Message-ID | <8bb40e3f-57ce-4d5a-a426-6b56cebc8de7@googlegroups.com> |
| In reply to | #102813 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-02-15 12:36 +0100 |
| Message-ID | <mailman.139.1455536203.22075.python-list@python.org> |
| In reply to | #102957 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Arjun Srivatsa <arjuns123@gmail.com> |
|---|---|
| Date | 2016-02-15 07:00 -0800 |
| Message-ID | <ea5105bf-68b9-4999-bbd6-631fd6408d99@googlegroups.com> |
| In reply to | #102960 |
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 = "10.52.124.135"
port = 27017
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
sys.stdout.write(s.recv(1024))
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.
[toc] | [prev] | [next] | [standalone]
| From | Arjun Srivatsa <arjuns123@gmail.com> |
|---|---|
| Date | 2016-02-15 07:28 -0800 |
| Message-ID | <18fabe32-b500-4b53-b6eb-e5419c8e530a@googlegroups.com> |
| In reply to | #102965 |
Okay,
I added
with open('yourfile','w') as f:
f.write(data)
to the read_server code in which the Actual data is stored in a file on my desktop. Then, it must be possible to read this file in the Write_db script to insert the data.
On Monday, February 15, 2016 at 4:00:37 PM UTC+1, Arjun Srivatsa wrote:
> 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 = "10.52.124.135"
> port = 27017
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.connect((host, port))
> sys.stdout.write(s.recv(1024))
>
>
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-02-15 17:33 +0100 |
| Message-ID | <mailman.145.1455554034.22075.python-list@python.org> |
| In reply to | #102965 |
Arjun Srivatsa wrote:
> 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 = "10.52.124.135"
> port = 27017
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.connect((host, port))
> sys.stdout.write(s.recv(1024))
>
>
> 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.
What I mean is once you have working scripts
connect_to_mongodb()
while True:
record = make_fake_data()
insert_record_into_mongodb(record)
and
connect_to_server()
while True:
record = read_record_from_server()
print(record)
you can combine the code in a third script to
connect_to_server()
connect_to_mongodb()
while True:
record = read_record_from_server()
insert_record_into_mongodb(record)
and be fairly sure that the combination works, too.
[toc] | [prev] | [next] | [standalone]
| From | Arjun Srivatsa <arjuns123@gmail.com> |
|---|---|
| Date | 2016-02-16 04:41 -0800 |
| Message-ID | <147512d7-ace9-4944-be2f-3eecb0ad525f@googlegroups.com> |
| In reply to | #102971 |
Thanks a lot. Will implement that. Although I am able to do using just 2 scripts as well.
On Monday, February 15, 2016 at 5:34:08 PM UTC+1, Peter Otten wrote:
> Arjun Srivatsa wrote:
>
> > 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 = "10.52.124.135"
> > port = 27017
> >
> > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> > s.connect((host, port))
> > sys.stdout.write(s.recv(1024))
> >
> >
> > 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.
>
> What I mean is once you have working scripts
>
> connect_to_mongodb()
> while True:
> record = make_fake_data()
> insert_record_into_mongodb(record)
>
> and
>
> connect_to_server()
> while True:
> record = read_record_from_server()
> print(record)
>
> you can combine the code in a third script to
>
> connect_to_server()
> connect_to_mongodb()
> while True:
> record = read_record_from_server()
> insert_record_into_mongodb(record)
>
> and be fairly sure that the combination works, too.
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2016-02-11 20:06 -0500 |
| Message-ID | <mailman.62.1455239211.22075.python-list@python.org> |
| In reply to | #102811 |
On Thu, 11 Feb 2016 07:12:06 -0800 (PST), Arjun Srivatsa
<arjuns123@gmail.com> declaimed the following:
>
># 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)
Why hard-code the address/port when you just previously defined them as
pseudo-constants IP and PORT?
>db = client.test_database
>
>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>s.bind((IP, PORT))
Why are you now binding a new socket to the same IP/PORT used by the
database object? Doesn't the "client" object already have a binding to the
database? Don't you then use methods of "client" and "db" to work with data
there?
>s.listen(1)
>
Isn't this waiting for some outside process to attempt to connect to
the same IP/PORT that you used to connect to the database?
>#connections loop
>while True:
>conn, addr = s.accept()
>print 'Connection address:',addr
>try:
># read loop
> while True:
> data = server.recv(BUFFER_SIZE)
Now I'm really confused -- you (failed -- since you didn't () invoke
it) to close the "server" connection above, but here you are trying to
receive data from it.
>
> if not data: break
> conn.sendall(data)
>
Who connected? After all, you tied yourself to the same IP/PORT as the
database?
>
> # send to MongoDB
>
> mongodoc = { "data": data, "date" : datetime.datetime.utcnow() }
>
>
> ABC = db.ABC
> ABC_id = ABC.insert_one(mongodoc).inserted_id
>
>finally:
> conn.close()
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web