How to check the country origin of a url - python-3.x

I have a list of url's and want to find out their country of origin via python 3. And was wondering if anyone could help.
For example: quikr.com or kooora.com
Thanks

You can use
geoip2
either via a database, you need to download (which I did) or create an account.
Thanks #AlexVorndran for the initial help.
Code example:
import geoip2.database
import socket
ip = socket.gethostbyname('nike.com')
reader = geoip2.database.Reader('GeoLite2-Country_20190305/GeoLite2-Country.mmdb')
response = reader.country(ip)
response.country.iso_code # Results in 'US'

Related

Read variables value from PLC with Python opcua-asyncio – BadNoMatch: “The requested operation has no match to return.”

Edited to correct some typos:
Hi I’m new to opcua-asyncio package so please be patient if I missed something really obvious, I’m trying to figure out how to connect to a Siemens S7 1200 CPU 1212C firmware version 14 PLC from Python (3.11) and read some variables values.
I’m sorry I can’t figure out a way to support my case with a reproducible example because it’s intrinsically connected to the test PLC I’m working with, if you can suggest a way to make it reproducible I’m going to amend the answer accordingly.
The structure on my test PLC is detailed in the picture, I can access it through different clients, the variables from t1 to t19 are my goal.
[PLC Structure][1]
I went through the documentation (especially the client minimal example ) and some stackoverflow answers and I wrote the following code
# modules
import asyncio
from asyncua import Client
# url and namespace
url = foo bar # the plc location on the network
namespace = "OPC-UA:PLC_1"
t2 =[]
# I can find the objects inside the node
async with Client(url=url) as client:
# Find the namespace index
nsidx = await client.get_namespace_index("urn:OPC-UA:PLC_1")
print(nsidx)
root = client.nodes.root
print("Root node is: ", root)
objects = client.nodes.objects
print("Objects node is: ", objects)
# I can’t find my variables
var = await client.nodes.root.get_child(
["0:Objects", "3:ServerInterfaces", "0:Face"])
print("Var is: ", var)
t2 = await var.get_children_descriptions()
print(t2)
By my understanding of the structure in OPC-UA:PLC_1 namespace I should find an “Objects” node object listing a “ServerInterfaces” node object listing a “Face” node object listing the variables from t1 to t19. However if I ask “Objects” to describe its childs through get_children_descriptions() I can see the ServerInterfaces node object however this Serverinterfaces appears to be named “Face”.
If I ask for the childrens of “Face” I receive a BadNoMatch error.
Any help pointing me to the right direction would be much appreciated. Thank you!
[1]: https://i.stack.imgur.com/QyQCJ.png
I think the Browsename namespace index of Face is wrong.
The index 0 is used for the base nodeset.
You can check the correct index with an other UA Client like the UAExpert (here you need to look at the attibute window not the address space window (which do not show the namespace index)).
If you defined the Face in the face "OPC-UA:PLC_1" the line should be:
var = await client.nodes.root.get_child(
["0:Objects", "3:ServerInterfaces", "3:Face"])

flight offer search not pulling all air fares

I would like to seek help on the codes below.
Apparently, the code works by pulling out air fares but it seems like it does not pull out all the available air fares that I can see directly from the airlines websites.
Could anyone please assist me to let me know what's happening? Thanks!
from amadeus import Client, ResponseError
Origin = "SIN"
Destination = "CDG"
amadeus = Client(hostname='production', client_id='HIDDEN', client_secret='HIDDEN')
try:
'''
Find the cheapest flights from Origin to Destination
'''
response = amadeus.shopping.flight_offers_search.get(
#originLocationCode=Origin, destinationLocationCode=Destination, departureDate='2023-04-04', adults=1)
originLocationCode=Origin, destinationLocationCode=Destination, departureDate='2023-03-03', returnDate='2023-04-04', travelClass='BUSINESS', includedAirlineCodes='SQ', currencyCode='SGD', adults=1)
print(response.data)
except ResponseError as error:
raise error
The Self-Service APIs only return public fares coming from the GDS. That means that you will find discrepancy for both the flights and the prices too comparing to some websites you might visit.
For more details check the section carriers and rates in the developer guides.

Function service_account.Credentials.from_service_account_info() not working

I'm writing an application based on GCP services and I need to access to an external project. I stored on my Firestore database the authentication file's informations of the other project I need to access to. I read this documentation and I tried to apply it but my code does not work. As the documentaion says, what I pass to the authentication method is a dictionary[str, str].
This is my code:
from googleapiclient import discovery
from google.oauth2 import service_account
from google.cloud import firestore
project_id = body['project_id']
user = body['user']
snap_id = body['snapshot_id']
debuggee_id = body['debuggee_id']
db = firestore.Client()
ref = db.collection(u'users').document(user).collection(u'projects').document(project_id)
if ref.get().exists:
service_account_info = ref.get().to_dict()
else:
return None, 411
credentials = service_account.Credentials.from_service_account_info(
service_account_info,
scopes=['https://www.googleapis.com/auth/cloud-platform'])
service = discovery.build('clouddebugger', 'v2', credentials=credentials)
body is just a dictionary containing all the informations of the other project. What I can't understand is why this doesn't work and instead using the method from_service_account_file it works.
The following code will give to that method the same informations of the previous code, but inside a json file instead of a dictionary. Maybe the order of the elements is different, but I think that it doesn't matter at all.
credentials = service_account.Credentials.from_service_account_file(
[PATH_TO_PROJECT_KEY_FILE],
scopes=['https://www.googleapis.com/auth/cloud-platform'])
Can you tell me what I'm doing wrong with the method from_service_account_info?
Problem solved. When I posted the question I manually inserted from the GCP Firestore Console all the info about the other project. Then I wrote the code to make it authomatically and it worked. Honestly I don't know why it didn't worked before, the informations put inside Firestore were the same and the format as well.

Does DNSpython have a method that automatically performs a forward or reverse lookup depending on the value that it's passed?

I am wondering if there is a way to pass either a host, fqdn, or IP address to DNSPython and have it perform the proper lookup (forward for hosts and fqdns and reverse for ips). Also, I want to know what kind of address (host, fqdn, or ip) was sent to the method originally.
Thanks in advance
To my knowledge, there's not a single function that will do what you're looking for. However, it wouldn't be too hard to implement. Here's how I'd probably do it.
First, I'd check if the input was an IP address
import ipaddress
def is_ipaddress(string):
try:
ipaddress.ip_address(string)
return True
except ValueError:
return False
If it is an IP address, then I'd call dns.query.reverse_query(). This is assuming I have installed the latest development version of dnspython from Github because reverse_query() was only recently added (see https://github.com/rthalley/dnspython/commit/7c105cce64699e1a221176f98f7cb9e682aba1e0).
If it's not an IP address, then I'd prepare my query with dns.message.make_query(name, rdtype) and then send it with dns.query.udp().
If you wanted to use the value of search in /etc/reolv.conf, you might consider creating a dns.resolver.Resolver, which currently does search processing by default.
import dns.resolver
import dns.rdatatype
resolver = dns.resolver.Resolver()
response = resolver.query('my-computer', dns.rdatatype.A)

infusionsoft contact field query with python

I know how to connect to Infusionsoft with Python 3 and how to process the following simple example:
#Set up the contact data we want to add
contact = {}; #blank dictionary
contact[“FirstName”] = “John”;
contact[“LastName”] = “Doe”;
contact[“Email”] = "john#doe.com";
contact[“Company”] = “ACME”;
But how do I mass update the WHOLE database? e.g. If I want to update ALL The Phone1 fields with an extra bit of code using IF statements.
Using Infusionsoft API you can only update contacts data one by one, sending a separate request per contact. Exact request depends on which type of API you use: REST or XML-RPC

Resources