2015年9月28日 星期一

取得實際對外IP的方法

這個是一個很簡單的小功能,對 client side 來說很有幫助,對server side 來說,也是超級簡單,所有的server side script 幾乎都支援,主要的問題就是要有一台Public IP 的 server.

官方網址:
https://www.ipify.org/

API說明:

API URIResponse TypeSample Output (IPv4)
https://api.ipify.orgtext98.207.254.136
https://api.ipify.org?format=jsonjson{“ip”:”98.207.254.136″}
https://api.ipify.org?format=jsonpjsonpcallback({“ip”:”98.207.254.136″});
https://api.ipify.org?format=jsonp&callback=getipjsonpgetip({“ip”:”98.207.254.136″});




使用範例:

Bash

#!/bin/bash

ip=$(curl -s https://api.ipify.org)
echo "My public IP address is: $ip"

Python

# 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://api.ipify.org').text
print 'My public IP address is:', ip

Ruby

require "net/http"

ip = Net::HTTP.get(URI("https://api.ipify.org"))
puts "My public IP Address is: " + ip

PHP

<?php
    $ip = file_get_contents('https://api.ipify.org');
    echo "My public IP address is: " . $ip;
?>

Perl

use strict;
use warnings;
use LWP::UserAgent;

my $ua = new LWP::UserAgent();
my $ip = $ua->get('https://api.ipify.org')->content;
print 'My public IP address is: '. $ip;

VB.NET

Dim client as New System.Net.WebClient
Dim ip as String = client.DownloadString("https://api.ipify.org")

NodeJS

var http = require('http');

http.get({'host': 'api.ipify.org', 'port': 80, 'path': '/'}, function(resp) {
  resp.on('data', function(ip) {
    console.log("My public IP address is: " + ip);
  });
});

Javascript

<script type="application/javascript">
  function getIP(json) {
    document.write("My public IP address is: ", json.ip);
  }
</script>

<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>

jQuery

<script type="application/javascript">
  $(function() {
    $.getJSON("https://api.ipify.org?format=jsonp&callback=?",
      function(json) {
        document.write("My public IP address is: ", json.ip);
      }
    );
  });
</script>

C#

using System;
using System.Net;

namespace Ipify.Examples {
    class Program {
        public static void Main (string[] args) {
            WebClient webClient = new WebClient();
            string publicIp = webClient.DownloadString("https://api.ipify.org");
            Console.WriteLine("My public IP Address is: {0}", publicIp);
        }
    }
}

Arduino

if (client.connect("api.ipify.org", 80)) {
    Serial.println("connected");
    client.println("GET / HTTP/1.0");
    client.println("Host: api.ipify.org");
    client.println();
} else {
    Serial.println("connection failed");
}


而且還有好心人幫忙多包了一層Object,  讓程式更好用:
ex:
https://github.com/rdegges/python-ipify
To install ipify, simply run:
$ pip install ipify
This will install the latest version of the library automatically.

Usage

Using this library is very simple. Here's a simple example:

>>> from ipify import get_ip
>>> ip = get_ip()
>>> ip
u'96.41.136.144'

沒有留言:

張貼留言

Facebook 留言板