When to Send IP Data: Privacy, Compliance, and Alternatives

Send IP Programmatically: Examples in Python, JavaScript, and Go

Sending an IP address programmatically is a common task in networking, logging, analytics, and access control. Below are practical, concise examples showing how to obtain and send the client or local machine IP address from server-side and client-side contexts using Python (Flask), JavaScript (browser and Node.js), and Go (net/http). Each example includes minimal error handling and focuses on typical patterns you can adapt.

Important note about privacy and security

Only collect and transmit IP addresses when you have a legitimate purpose and proper consent; treat them as potentially personal data and protect them in transit and at rest.

1) Server-side: Python (Flask) — capture client IP and forward it

  • Use when handling incoming HTTP requests and you need the client’s IP for logging, rate limiting, or forwarding to another service.
python
# requirements: flask, requestsfrom flask import Flask, request, jsonifyimport requestsimport os app = Flask(name)DEST_URL = os.getenv(“DEST_URL”, “https://example.com/receive-ip”) def get_client_ip(): # Prefer X-Forwarded-For when behind proxies (first entry is original client) xff = request.headers.get(“X-Forwarded-For”, “”) if xff: return xff.split(“,”)[0].strip() return request.remote_addr @app.route(“/report”, methods=[“POST”])def report(): client_ip = get_client_ip() payload = {“ip”: client_ip} try: r = requests.post(DEST_URL, json=payload, timeout=5) r.raise_for_status() except requests.RequestException as e: return jsonify({“status”: “error”, “detail”: str(e)}), 502 return jsonify({“status”: “ok”, “ip_sent”: client_ip})

Notes:

  • If your app is behind a proxy/load balancer, ensure it sets X-Forwarded-For and that your proxy is trusted.
  • Send over HTTPS and authenticate requests to the destination service.

2) Client-side: JavaScript (Browser) — get public IP then send it

  • Browsers cannot directly read the machine’s network IP in many contexts; to get the public IP, call an external IP service, then send it to your server.
javascript
// Example: fetch public IP then POST to your endpointasync function sendPublicIP(endpoint = “/client-ip”) { try { const ipRes = await fetch(”https://api.ipify.org?format=json”); if (!ipRes.ok) throw new Error(“Failed to fetch IP”); const { ip } = await ipRes.json(); const res = await fetch(endpoint, { method: “POST”, headers: { “Content-Type”: “application/json” }, body: JSON.stringify({ ip }), }); return await res.json(); } catch (err) { console.error(“sendPublicIP error:”, err); throw err; }}

Notes:

  • Use a trusted IP service (or your own endpoint) and HTTPS.
  • For local/private IPs (192.168.x.x, etc.), browser access is limited for privacy; WebRTC can expose local IPs but modern browsers restrict this.

3) Server-side: Node.js (Express) — capture client IP and POST it elsewhere

  • Similar to the Flask example, using Express and node-fetch/axios.
javascript
// requirements: express, node-fetch (or axios)const express = require(“express”);const fetch = require(“node-fetch”);const app = express();app.use(express.json());const DEST_URL = process.env.DEST_URL || “https://example.com/receive-ip”; function getClientIp(req) { const xff = req.headers[“x-forwarded-for”]; if (xff) return xff.split(“,”)[0].trim(); return req.connection.remoteAddress || req.socket.remoteAddress;} app.post(“/report”, async (req, res) => { const ip = getClientIp(req); try { const r = await fetch(DEST_URL, { method: “POST”, headers: { “Content-Type”: “application/json” }, body: JSON.stringify({ ip }), timeout: 5000, }); if (!r.ok) throw new Error(`Downstream error ${r.status

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *