Trying to create a TCP client using Python to test the backend connectivity of any host.

Compilation of script py://connect.py failed.
python script:

import socket

def establish_tcp_connection(host, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
print(f"TCP connection established with {host}:{port}")
sock.close()
except socket.error as e:
print(f"Error: {e}")

# Example usage
var host= flow.getVariable('targethost');
var port= flow.getVariable('targetport');

establish_tcp_connection(host, port)

the host and port values can be taken from the request. I am getting a compilation error while trying to deploy the proxy with the above script.


Will this work or is there a different way i can approach this.

0 3 410
3 REPLIES 3

Don't do that.  It wont work, and That's not what the python policy is for. If you want to build your own gateway, with upstream monitoring and so on, maybe you should be using Envoyproxy, or the Springboot framework, or something like that.

thanks for the response, 

So we are trying to create a functionality in our inbuilt portal for users to check the connectivity from apigee >> backendhosts.

The portal will be consuming the apigee proxy, do u see a different way we can approach this, before we used to have nodejs doing it within the proxy, too bad its depricated. since python is not an option thing of moving it to java script http cleint.

Most customers that I know, expose a healthcheck API.  Ping the proxy at the /healthcheck endpoint and it responds. Some customers define multiple levels of healthcheck, so that /healthcheck1 is a loopback on the API proxy, and /healthcheck2 results in a call to a target system (behind Apigee).  And if there is a persistence layer, one could imagine adding /healthcheck3 , which would instruct the upstream system to read its datastore. 

There is no need for python, nodejs or JavaScript inside the Apigee proxy for any of this. Just a regular API that is defined to do "nothing" except verify connectivity.  This is much closer to what you want anyway.