How to connect to a Lotus-Notes database with Python? - python

I have to extract data from a Notes database automatically for a data pipeline validation.
With HCL Notes I was able to connect to the database, so I know the access works.
I have the following information to access the database:
host (I got both hostname and ip address), domino server name, database name (.nsf filename)
I tried the noteslib library in the below way:
import noteslib
db = noteslib.Database('domino server name','db filename.nsf')
I also tried adding the host to the server parameter instead, but it does not work.
I receive this error:
Error connecting to ...Double-check the server and database file names, and make sure you have
read access to the database.
My question is how can I add the host and the domino server name as well (if it is required)?
Notes HCL authenticates me before accessing the database using the domino server name and the .nsf file. I tried adding the password parameter to the end, but also without luck. I am on company VPN, so that also should not be an issue.

In Order for noteslib to work you need an installed and configured HCL Notes Client on that machine. Only with an installed Notes Client the needed COM registrations and the dlls to connect to Domino are present.
In addition the Notes Client and the python version you are using need to be the same bitness: If Notes Client is 32Bit then python needs to be 32Bit. If Notes Client is 64Bit (only available since 12.0.2) then python needs to be 64Bit as well.
As soon as this requirement is met, you can simply use your example by adding the password parameter as a third parameter to your command:
db = noteslib.Database('domino server name','db filename.nsf', 'yourIDPassword')
If you still get an error when connecting to the server then you might need to put the server common name and its IP address into your hosts file.
So if your Domino- Servername is
YourServer/YourOrganization
and the IP address of that server is
192.168.1.20
then you put this into your hosts:
yourserver 192.168.1.20

You can connect using com on windows.
I use this python library https://pypi.org/project/pywin32/
import win32com.client
import sys
notesServer = "Servername/Domain"
notesFile = "file.nsf"
notesPass = ""
#Connect to notes database on server
notesSession = win32com.client.Dispatch('Lotus.NotesSession')
notesSession.Initialize(notesPass)
notesDatabase = notesSession.GetDatabase(notesServer,notesFile)

Related

DB2 : Python ibm_db2 connecting, which port to use?

I am attempting to connect to a remote db2 instance. I seem to be having connection port issues or protocol issues. Below is a sample connection setting. What is the default connection port using TCPIP & python? I am reaching the server but unable to create a connection to database. Database exists.
connection = ibm_db.connect("DATABASE=DATABASE_NAME;HOSTNAME=host;PORT=50000;PROTOCOL=TCPIP;UID=username;PWD=password;", "", "")
Im receiving the following error:
Exception: [IBM][CLI Driver] SQL30061N The database alias or database name "DATABASE_NAME " was not found at the remote node. SQLSTATE=08004 SQLCODE=-30061
The error message seems clear but the cause might vary. Most likely either the database-name or the port-number is incorrect.
You get that message if a Db2-server responded indicating Db2 cannot find the specified database on HOSTNAME in the Db2-instance listening on the specified port-number.
A Db2-LUW hostname might have more than one Db2-instance running concurrently (each listening on different port-numbers), according to the hardware-resources available.
A Db2-Linux/Unix/Windows instance can have many physical databases inside it, each with a distinct name and one or more aliases.
Ask your DBA or a colleague for the correct database-name and port-number per hostname.
Alternatively ssh (or remote-desktop) to that hostname, find the owner (userid) of the process listening on port 50,000 (or whatever port you are using), become that userid (for Linux/Unix: use su or sudo ) and use db2 list db directory command to show local databases in that Db2-instance. For Db2-servers on Windows: start > db2cwadmin.bat > db2 list db directory . On Linux/Unix, use ps -ef | grep db2sysc to see how many Db2-instances are running and you can use that information (along with netstat) to discover the port on which they are listening.

Trying to access Postgres Data within the VPN without adding local machine ip to Postgres Server

I have a project using pandas-python to access data from Postgres using SQLAlchemy createngine function. While I pass the credentials and hostname:portname it throws error and asks me to add the machine IP to pg_conf.hba file on the Postgres server. Which will be cumbersome as I don't have a static IP for my machine and even this project need to be shared with other people and it doesn't make any sense to keep on adding new IPs or making requests with ** IPs as it has sensitive data.
Additional information on the topic revealed that actual issue being the
local address he client is using for sending data when talking to the (database) server:
Your client need to use the local VPN address assigned as source address.
This is achieved by adding in a socket.bind(_source address_) call before the call to socket.connect(_target address_).
Or, more conveniently, just provide the source address parameter with the socket.create_connection(address[, timeout[, source_address]]) call that is setting up the connection to the server.

Remote access to mySQL using Python

I have trouble connecting to my mySQL database remotely through Python.
I use the following to connect to mySQL:
import mysql.connector
cnx = mysql.connector.connect(host='XXX.XXX.XXX.X',user='XXXX',password='XXXXXX',database='testdb')
But I get the following error:
2003: Can't connect to MySQL server on '%HOST%:3306' (10060 A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)
The server is running and when I run the same code on the computer I run the server from using 'localhost'
import mysql.connector
cnx =
mysql.connector.connect(host='localhost',user='XXXX',password='XXXXXX',database='testdb')
it works and I can modify the data in the database. I'm trying to connect it remotely from another computer though.
I've tried using GRANT ALL ON *.* TO User#Host IDENTIFIED BY 'password'; but no result. I checked my firewall and allowed all incoming and outgoing connections through port 3306 which is used by default.
I'm new to mySQL and really have no clue what to do. I don't even know if I use the correct hostname :') I use the IP address of the computer I run the server from,I think that's right.
You dont need to GRANT ALL privilage to the user. You need to tell MYSQL that this user is allowed to login from a remote location.
In fact as you are allowing remote access through this user account now, you should make sure that it can access only the database(s) it needs to, and definitely cannot use GRANT
For example
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypass';
Will allow this user to connect from any ip address. To be more secure you should try to be more specific and specify a individual ip address if you can
CREATE USER 'myuser'#'11.22.33.44' IDENTIFIED BY 'mypass';
Remember, you are creating a new user account here because you already have a
myuser#localhost
Either way you need to make sure that the password is a strong one, specially is you use the % any ip option

Connecting to IB TWS using IBpy

I downloaded the standalone TWX for Mac OS X. Then I also installed IBpy through pip. I have the TWX open and I ran the following lines:
from ib.opt import ibConnection
con = ibConnection()
print(con.connect())
However, it prints False. What am I doing wrong? In TWX I have the localhost IP 127.0.0.1 as a trusted address.
You also have to check "enable activex and socket clients" in the API settings.
There may be a bug in newer versions 950-952 where you don't specify 127.0.0.1, but instead check the box where it says "allow connections from localhost only". I haven't tested that, but have read about it.
If you use IB gateway, "enable socket clients" isn't required as the only way to use it is with an API. Note that gateway is port 4001 by default. Use con = ibConnection(port=4001,clientId=123)
You're connecting with clientId 0, you could use con = ibConnection(123) for example to use a different clientId.
Don't forget to call con.disconnect() to close the connection or the server won't allow you to re-connect using that id.
add: here's the bug I read about at IB-API yahoo user group.
But as soon as I uncheck ‘Allow connections from localhost only’, it
won’t accept connections from any address, not even local ones (ie
even with 127.0.0.1 as a Trusted IP address).

Why is PyMongo 3 giving ServerSelectionTimeoutError?

I'm using:
Python 3.4.2
PyMongo 3.0.2
mongolab running mongod 2.6.9
uWSGI 2.0.10
CherryPy 3.7.0
nginx 1.6.2
uWSGI start params:
--socket 127.0.0.1:8081 --daemonize --enable-threads --threads 2 --processes 2
I setup my MongoClient ONE time:
self.mongo_client = MongoClient('mongodb://user:pw#host.mongolab.com:port/mydb')
self.db = self.mongo_client['mydb']
I try and save a JSON dict to MongoDB:
result = self.db.jobs.insert_one(job_dict)
It works via a unit test that executes the same code path to mongodb. However when I execute via CherryPy and uWSGI using an HTTP POST, I get this:
pymongo.errors.ServerSelectionTimeoutError: No servers found yet
Why am I seeing this behavior when run via CherryPy and uWSGI? Is this perhaps the new thread model in PyMongo 3?
Update:
If I run without uWSGI and nginx by using the CherryPy built-in server, the insert_one() works.
Update 1/25 4:53pm EST:
After adding some debug in PyMongo, it appears that topology._update_servers() knows that the server_type = 2 for server 'myserver-a.mongolab.com'. However server_description.known_servers() has the server_type = 0 for server 'myserver.mongolab.com'
This leads to the following stack trace:
result = self.db.jobs.insert_one(job_dict)
File "/usr/local/lib/python3.4/site-packages/pymongo/collection.py", line 466, in insert_one
with self._socket_for_writes() as sock_info:
File "/usr/local/lib/python3.4/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.4/site-packages/pymongo/mongo_client.py", line 663, in _get_socket
server = self._get_topology().select_server(selector)
File "/usr/local/lib/python3.4/site-packages/pymongo/topology.py", line 121, in select_server
address))
File "/usr/local/lib/python3.4/site-packages/pymongo/topology.py", line 97, in select_servers
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: No servers found yet
We're investigating this problem, tracked in PYTHON-961. You may be able to work around the issue by passing connect=False when creating instances of MongoClient. That defers background connection until the first database operation is attempted, avoiding what I suspect is a race condition between spin up of MongoClient's monitor thread and multiprocess forking.
As mentioned here: https://stackoverflow.com/a/54314615/8953378
I added ?ssl=true&ssl_cert_reqs=CERT_NONE to my connection string, and it fixed the issue.
so instead of:
connection_string = "mongodb+srv://<USER>:<PASSWORD>#<CLUSTER>/<COLLECTION>"
I wrote:
connection_string = "mongodb+srv://<USER>:<PASSWORD>#<CLUSTER>/<COLLECTION>?ssl=true&ssl_cert_reqs=CERT_NONE"
(Note that if you have other parameters in your connection string, you need to change the ? to & )
I am not sure if you are using the MongoDB paired with AWS Cloud service. But if you are, I found that you have to specify which IP Address you want MongoDB to have access to.
So what you need to do is add the IP Address of your host server to allow entry.
In MongoAtlas, this can be done at this page
I know there was already a solution to the same issue, but I didn't find a solution that helped my situation, so wanted to post this, so others could benefit if they ever face the same problem that I do.
I fixed it for myself by downgrading from pymongo 3.0 to 2.8. No idea what's going on.
flask/bin/pip uninstall pymongo
flask/bin/pip install pymongo==2.8
I had the same problem with Pymongo 3.5
Turns out replacing localhost with 127.0.0.1 or corresponding ip address of your mongodb instance solves the problem.
I solved this by installing dnspython (pip install dnspython). The issue is that: "The "dnspython" module must be installed to use mongodb+srv:// URIs"
Go to your Atlas Console > Network Access, then add your client IP address,
Ex. 0.0.0.0/00
(Note: All client ips can access your database)
In my case
I was using Mongo Atlas
I got another IP adress after a router reboot
hence I had to add that IP to the whitelist on Mongo Atlas settings via
MongoAtlas website -> Network Access -> IP Whitelist -> Add IP Address -> Add Current IP Address
then wait for IP Address's status to change to Active and then try to run the app again
if you are using a repl.it server to host, just add the host ip you used to configure your server, for me it was 0.0.0.0, which is the most common
I was facing the same exception today. In my case, the proxy settings were probably blocking the connection since I could establish a successful connection to the mongodb by changing my wifi. Even if this question is marked as solved already, it can hopefully narrow down the problem for some others.
I've come accross the same problem and finally I found that the client IP is blocked by the firewall of the mongo server.
I encountered this too.
This could be due to pymongo3 isn't fork safe.
I fix this by adding --lazy-apps param to uwsgi, this can avoid the "fork safe" problem.
seeing uwsgi doc preforking-vs-lazy-apps-vs-lazy.
Notice, no sure for this two having positive connection.
I simply added my current IP address in the network access tab, as it got changed automatically. Deleted the earlier one, there was a slight change in IP address.
pymongo 3 will not tell you your connection failed when you instantiate your client. You may not be connected.
https://api.mongodb.com/python/3.5.1/api/pymongo/mongo_client.html
"it no longer raises ConnectionFailure if they are unavailable ..
You can check if the server is available like this:"
from pymongo.errors import ConnectionFailure
client = MongoClient()
try:
# The ismaster command is cheap and does not require auth.
client.admin.command('ismaster')
except ConnectionFailure:
print("Server not available")
maybe you can try to add your server ip address into the mongod.conf file.
if you use linux(ubuntu) os,you can try my solution:
modify mongod.conf file:
vi /etc/mongod.conf
and you can add mongodb server ip address behind 127.0.0.1,and save:
net:
port:27017
bindIp:127.0.0.1,mongodb server ip
in the teminal:
sudo service mongod restart
Now,you can try to connect mongodb by using pymongo MongoClient.
That error has occurred because there is no MongoDB server running in the background. To run the MongoDB server open cmd or anaconda prompt and type this:-
"C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"
then run
import pymongo
myclient = pymongo.MongoClient()
mydb = myclient["mydatabase"]
myclient.list_database_names()
I'm using pymongo 3.2 and I run into the same error, however it was a missconfiguration in my case. After enabling authorization, I forgot to update the port in the url which ended up in a connection timout. Probably it is worth to mention that ?authSource might be required as it is typically different than the database storing the application data.
I commented out bindIP variable in mongod.conf instead of allowing all connections (for which you have to enter 0.0.0.0). Of course, beware of the consequence.
The developers are investigating this problem, tracked in PYTHON-961. You may be able to work around the issue by running mongod.exe manually and monitoring it. This issue arises when the console freezes and you can hit the enter if the mongod console is got stuck. This is the simplest solution for now until the developers fix this bug.
I ran into the same issue during development. It was due to the fact that mongodb wasn't running on my local machine (sudo systemctl restart mongod to get mongodb running on Ubuntu).
I faced the same error on windows and I just started the MongoDB service
open services ctrl+R then type services.msc then Enter
For my case I only set my ip allow list 0.0.0.0 allow anywhere but you can set your ip using "what is my ip" and copy paste it to network access > add ip
I have been struggling with same problem. Read and either insert did not work at all failed with ServerSelectionTimeoutError.
I have been using pymongo==3.11.4 on Ubuntu 18.04 LTS.
Tried use connect=False, pass extra ?ssl=true&ssl_cert_reqs=CERT_NONE options to my connection string and other suggestions listed above. In my case they didn't work.
Finally simple tried to upgrade to pymongo==3.12.1 and connection started to work without passing connect=false, and other extra arguments suggested.
login = '<USERNAME>'
password = '<PASSWORD>'
host = '*.mongodb.net'
db = '<DB>'
uri = f'mongodb+srv://{login}:{password}#{host}/{db}?retryWrites=true&w=majority'
client = MongoClient(uri, authsource='admin')#, connect=False)
collection = client.db.get_collection('collection_name')
# t = collection.find_one({'hello': '1'})
t = collection.insert_one({'hello': '2'})
print(t)
Make sure you entered the user password, not the MongoDB account password. I encountered similar issue. In my case, I mistakenly entered the MongoDB account password instead of the user password.
I had this issue today. I managed to deal with it by:
installing dnspython library > going to MongoDB webpage > signing in > security > network access > add IP address > adding the IP address from where my request comes from.
Hope this could help someone.
I had the same issue..the code that was working perfectly fine 2 minutes before gave this error. I was looking for solutions over google for about 30 minutes and it automatically got fixed. The problem could be my home internet connection. Just a guess but if you haven't made any changes to the code or any other config file best to wait for sometime and retry.
I was also facing the same issue. Then, I added
import certifi
Client = MongoClient("mongodb+srv://<username>:<password>#cluster0.ax9ugoz.mongodb.net/?retryWrites=true&w=majority", tlsCAFile=certifi.where())
and it solved my issue.
Certifi provides a collection of Root Certificates for validating the trustworthiness of SSL certificates while verifying the identity of TLS hosts.
This has been fixed in PyMongo with this pull_request.
This problem solved when I just toggled the MongoDB in the services to running which was stopped previously.
First set up the MongoDB environment.
Run this on CMD - "C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"
Open another CMD and run this - "C:\Program Files\MongoDB\Server\3.6\bin\mongo.exe"
And then you can use pymongo [anaconda prompt]
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client.test_db
collection = db['test_coll']
Refer - https://docs.mongodb.com/tutorials/install-mongodb-on-windows/

Categories