Connecting to IB TWS using IBpy - python

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).

Related

How to form an OPCUA connection in python from server IP address, port, security policy and credentials?

I have never used OPC-UA before, but now faced with a task where I have to pull data from a OPC-UA machine to push to a SQL database using python. I can handle the database part, but how to basically connect to the OPCUA server when I have only the following fields available?
IP address 192.168.38.94
Port 8080
Security policy: Basic256
Username: della_client
Password: amorphous##
Some tutorials I saw directly use a url, but is there any way to form the URL from these parameters, or should I ask the machine owners something more specific to be able to connect? I just want to be sure of what I need before I approach him.
Related, how to use the same parameters in the application called UA-Expert to verify the connections as well? Is it possible?
If it is relevant, I am using python 3.10 on Ubuntu 22.04.
You need to know which protocol is used. Then you can create the URLs by using the IP address as domain:
OPC UA binary: opc.tcp://ip:port
https https://ip:port
OPC UA WebSockets opc.wss://ip:port
http http://ip:port (Deprecated in Version 1.03)
In your example this could be opc.tcp://192.168.38.94:8080 or https://192.168.38.94:8080
In most cases, the binary protocol is used. But the port 8080 is a typical http(s) port.
The credential and the securityPolice are needed later in the connection process.
And yes: You can test the URLs with the UaExpert. You can finde a step-by-step tutorial in the documention

How to connect to a Lotus-Notes database with 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)

Why "[Errno 61] Connection refused" when program is listening on correct port, socket is bound to all interfaces?

I'll try be concise, but please let me know if I can provide any more helpful pieces of information.
I have client and server Python programs, and they work fine when ran on the same machine, and when the client connects to my machine's local IP (not 127.0.0.1, but the IP assigned to my machine). I have not been able to get this to work with my public IP.
I get a [Errno 61] Connection refused error when I try to get the client to connect to my router's public IP address. My server binds to all interfaces using bind(("0.0.0.0", 50000)), and I already set up port forwarding for my router. I verified that the program is listening on that port by running netstat -an | grep LISTEN and finding the following line:
tcp4 0 0 *.50000 *.* LISTEN
I can also seemingly reach the port through an online port checking tool, which shows that the port is open when I am running my program, and closed when I close that program. My program also registers the connection from this tool.
The fact that my program accepts the connection from the port checking tool gives me the impression that my client code is missing something, but I can't find any answers. It might be worth noting that I am still running my server and client code on the same machine, but I'm not sure why that would derail things. Here's the code I use to connect on the client side:
tcp_client = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
tcp_client.connect(('my_public_ip', 50000))
Are there any diagnostic steps that I can follow to narrow down my issue?
Before you spend any more time on this, try connecting to your public ip from a computer outside your home network. Spend a couple of dollars on an AWS instance for an hour if you have to, or try connecting from a friend's machine, whatever. It will probably work just fine.
I suspect the problem is simply that you cannot, from inside your home network, connect to your router's public ip address. I tried the same thing with my local network and ran into the same behavior.
If you really need to your public ip during development, you can just assign that as an alias to one of your local interfaces (ip addr add 1.2.3.4/32 dev eth0)...but it's probably easier just to use your an address on your local network, or just arrange for regular access to a remote system for testing.

Connecting to MongoDb DB that is installed in different server

We have to servers. I have installed MongoDB on one of the servers (UBUNTU - Digital Ocean VPS).
When I run a script to retrieve data from the same server using a localhost, I can do that perfectly.
import pymongo
//SERVER = 'mongodb://localhost:27017/myproject'
SERVER = 'mongodb://root:password#x.x.x.x:27017/myproject' where x.x.x.x is the address of my server
connection=pymongo.MongoClient(SERVER)
db = connection.myproject
print list(db.coll.find())
The problem is thqt I can't connect to this DB. Note that I can ssh and run the script using localhost inside the server; but not the case out of the server.
Do I need to go through some configuration:
You must allow remote access
vi /etc/mongod.conf
Listen only local interface.
bind_ip = 127.0.0.1
you must add the IP of your other servers. For Example:
Listen local interface and 192.168.0.100.
bind_ip = 127.0.0.1, 192.168.0.100
Comment out to listen on all interfaces
Nota: Comma Separated
I hope to help
For development purposes you can open an ssh tunnel like
ssh <UBUNTU - Digital Ocean VPS> -L27018:localhost:27017
and then connect to the remote db as
SERVER = 'mongodb://root:password#localhost:27018/myproject'
while ssh connection remains open. You can use any free port instead of 27018.
Otherwise you need to reconfigure mongodb to listen to all interfaces. Comment out bindIp line in mongodb config and restart the server. This will make the DB publicly accessible, so make sure you use strong passwords and don't allow anonymous access.
Finally, if you are using VPN, you need to uncomment bindIp line in the mongodb config, and add VPN interface there, e.g.:
bindIp = 127.0.0.1,10.0.1.12
where 10.0.1.12 should be replaced with vpn interface of your ubuntu box. You can find exact value with ifconfig. Important: there are no spaces around coma.

difference between finding ip address by python an cmd

I wrote this code for finding google ip in python
import socket
print socket.gethostbyname('google.com')
.
.
173.194.39.0
but if we use command prompt and ping command for finding google ip result is:216.58.208.36
why there is difference between two results?
Both of those IP addresses resolve to Google.com. We can verify this from the command line with the unix whois command.
$ whois 216.58.208.36
NetRange: 216.58.192.0 - 216.58.223.255
CIDR: 216.58.192.0/19
NetName: GOOGLE
$ whois 173.194.39.0
NetRange: 173.194.0.0 - 173.194.255.255
CIDR: 173.194.0.0/16
NetName: GOOGLE
I ran into this same issue and the cause was that the first command that required an IP address was using a cached DNS entry (because the DNS entry's time to live (TTL) hadn't expired yet) and then by the time the second command was issued the TTL had expired on the cached entry so a new DNS request was made for the domain therefore grabbing a new IP address from the DNS server which happened to be different because the domain had a lot of IP addresses just like Google.com.
Python just relies on the Operating System's DNS resolver (or whatever daemon is running) and as far as I know the socket module doesn't give you the ability to clear the DNS cache before it tries to resolve an address. If you want more control over this functionality you can use DNSPython or something similar. If you are using a daemon for DNS on your operating system (like on Linux, for example) then usually restarting the daemon will force a flush of DNS cache and you find both addresses to the be same (unless you run into the timing issue as described above with the TTL's expiring).
Hostnames are translated to IP addresses through something called a DNS server. When you type a name into a web browser or use a program such as ping, the hostname that you provide (google.com) eventually reaches an authoritative DNS server for that domain-separate from the server that you correspond with for the actual content.
google.com has multiple different servers that can respond to data requests. Depending on the implementation of the different programs you are using to generate the request and other factors such as the network traffic at the time that you make the request, multiple requests from the same host may be directed to different servers by the authoritative DNS server. This is accomplished by returning different IP addresses to your machine.
FWIW, both ping and socket.gethostbyname() for google.com resolve to 216.58.217.14 on my machine, running OS X Yosemite.

Categories