This repository contains the Python script which you can use to run on Azure Functions to obtain your public IPv4 address. The code is stored in one file, which uses HTTP X-Forwarded-For
header for IP identifying.
Full instruction how exactly this code is working is available here
You can get an output in a different formats. List of currently available formats are stored in the table below:
API URL | Response Type | Sample Output (IPv4) |
---|---|---|
https://showip.azurewebsites.net/api/http | text | 1.2.3.4 |
https://showip.azurewebsites.net/api/http?format=json | json | {"ip":"1.2.3.46"} |
https://showip.azurewebsites.net/api/http?format=jsonp | jsonp | callback({"ip":"1.2.3.4"}); |
This section contains some common usage patterns from a variety of programming languages.
#!/bin/bash
ip=$(curl -s https://showip.azurewebsites.net/api/http)
echo "My public IP address is: $ip"
ip=`curl -s https://showip.azurewebsites.net/api/http`
echo("My public IP address is: $ip")
# This example requires the requests library be installed. You can learn more
# about the Requests library here: http://docs.python-requests.org/en/latest/
from requests import get
ip = get('https://showip.azurewebsites.net/api/http').text
print('My public IP address is: {}'.format(ip))
require "net/http"
ip = Net::HTTP.get(URI("https://showip.azurewebsites.net/api/http"))
puts "My public IP Address is: " + ip
<?php
$ip = file_get_contents('https://showip.azurewebsites.net/api/http');
echo "My public IP address is: " . $ip;
?>
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("https://showip.azurewebsites.net/api/http").openStream(), "UTF-8").useDelimiter("\\A")) {
System.out.println("My current IP address is " + s.next());
} catch (java.io.IOException e) {
e.printStackTrace();
}
use strict;
use warnings;
use LWP::UserAgent;
my $ua = new LWP::UserAgent();
my $ip = $ua->get('https://showip.azurewebsites.net/api/http')->content;
print 'My public IP address is: '. $ip;
var httpClient = new HttpClient();
var ip = await httpClient.GetStringAsync("https://showip.azurewebsites.net/api/http");
Console.WriteLine($"My public IP address is: {ip}");
Dim httpClient As New System.Net.Http.HttpClient
Dim ip As String = Await httpClient.GetStringAsync("https://showip.azurewebsites.net/api/http")
Console.WriteLine($"My public IP address is: {ip}")
package main
import (
"io/ioutil"
"net/http"
"os"
)
func main() {
res, _ := http.Get("https://showip.azurewebsites.net/api/http")
ip, _ := ioutil.ReadAll(res.Body)
os.Stdout.Write(ip)
}
(require net/url)
(define ip (port->string (get-pure-port (string->url "https://showip.azurewebsites.net/api/http"))))
(printf "My public IP address is: ~a" ip)
;This example requires the drakma http package installed.
;It can be found here: http://www.weitz.de/drakma
(let ((stream
(drakma:http-request "https://showip.azurewebsites.net/api/http" :want-stream t)))
(let ((public-ip (read-line stream)))
(format t "My public IP address is: ~A" public-ip)))
Dim s As New HTTPSecureSocket
Dim t As String = s.Get("https://showip.azurewebsites.net/api/http",10)
MsgBox "My public IP Address is: " + t
val addr = scala.io.Source.fromURL("https://showip.azurewebsites.net/api/http").mkString
println(s"My public IP address is: $addr")
<script type="application/javascript">
function getIP(json) {
document.write("My public IP address is: ", json.ip);
}
</script>
<script type="application/javascript" src="https://showip.azurewebsites.net/api/http?format=jsonp&callback=getIP"></script>
<script type="application/javascript">
$(function() {
$.getJSON("https://showip.azurewebsites.net/api/http?format=jsonp&callback=?",
function(json) {
document.write("My public IP address is: ", json.ip);
}
);
});
</script>
using System;
using System.Net;
namespace PublicIP.Examples {
class Program {
public static void Main (string[] args) {
WebClient webClient = new WebClient();
string publicIp = webClient.DownloadString("https://showip.azurewebsites.net/api/http");
Console.WriteLine("My public IP Address is: {0}", publicIp);
}
}
}
:inets.start
{:ok, {_, _, inet_addr}} = :httpc.request('http://showip.azurewebsites.net/api/http')
:inets.stop
import HttpClient
var ip = newHttpClient().getContent("https://showip.azurewebsites.net/api/http")
echo("My public IP address is: ", ip)
$ip = Invoke-RestMethod -Uri 'https://showip.azurewebsites.net/api/http?format=json'
"My public IP address is: $($ip.ip)"
http.Fetch("https://showip.azurewebsites.net/api/http", function(body) print("My ip is: " .. body ) end
PureBasic
InitNetwork()
*Buffer = ReceiveHTTPMemory("https://showip.azurewebsites.net/api/http?format=json")
If *Buffer
ParseJSON(0, PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8))
FreeMemory(*Buffer)
Debug GetJSONString(GetJSONMember(JSONValue(0), "ip"))
EndIf
put "My public IP address is" && url "https://showip.azurewebsites.net/api/http"
NSURL *url = [NSURL URLWithString:@"https://showip.azurewebsites.net/api/http/"];
NSString *ipAddress = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"My public IP address is: %@", ipAddress);
import Foundation
let url = URL(string: "https://showip.azurewebsites.net/api/http")
do {
if let url = url {
let ipAddress = try String(contentsOf: url)
print("My public IP address is: " + ipAddress)
}
} catch let error {
print(error)
}