API access via python script

DanScho
New Contributor III

Hello,

I try to access an API via a python script.

I have access using postman. But if I try to access it via python in databricks, it will not work (timeout).

Here is my code:

import requests
import socket

headers = {
    'Content-Type': 'application/json',
    'x-api-key': '<api-key>',
    'User-Agent': 'PostmanRuntime/7.43.0',
    'Accept': '*/*',
    'Host': '<URL to host>',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'Content-Length': '105'
}
payload = {
    "result_l": "en",
    "result_n": 2,
    "texts": [
        "Text1.",
        "Text2"
    ]
}
 
    response = requests.post(url, headers=headers, json=payload, timeout=10)  # Timeout nach 10 Sekunden
    response.raise_for_status()  # Löst eine Ausnahme bei HTTP-Fehlern aus
    data = response.json()
    print(data)
 

Best regards,

Daniel

Walter_C
Databricks Employee
Databricks Employee

Are you running this from a notebook? if yes can you run bash command:

%sh

nc -vz prod-api.xsp.cloud.corpintra.net 443

I want to confirm your cluster is able to reach the endpoint.

DanScho
New Contributor III

yes, i use it from a notebook.

C:\Windows>nc -vz prod-api.xsp.cloud.corpintra.net 443
Der Befehl "nc" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

DanScho
New Contributor III

is "nc" a linux command? I use windows...

Walter_C
Databricks Employee
Databricks Employee

Sorry I am confused, you mentioned that you are running this in a notebook, this inside Databricks and running it through a cluster? if this is the case the command needs to be ran inside the notebook, you can add a new cell from it.


DanScho
New Contributor III

sorry for confusing... i missunderstood your question.... now I got it.

running this command in a notebook in databricks shows no result.... it is "loading" endlessly... I only can interrupt it....

DanScho
New Contributor III

now it shows:
nc: connect to prod-api.xsp.cloud.corpintra.net (53.13.186.164) port 443 (tcp) failed: Connection timed out

Walter_C
Databricks Employee
Databricks Employee

ok that means that your subnet for clusters in the cloud deployment is not allowing connections to this host on port 443, you will need to check with your cloud administrator or network team to allow this traffic 

DanScho
New Contributor III

hmm... not good... is it not possible to configure this in azure databricks by myself?

Walter_C
Databricks Employee
Databricks Employee

Unfortunately no because this is not being blocked by Databricks, this is being blocked by a firewall or security group at the cloud level

View solution in original post

DanScho
New Contributor III

Nevertheless: Thank you very much for helping me!