如何获取公网IP

点点圈 提交于 2020-02-27 05:04:46

地址: http://ip.wang-li.top:93/4u6385IP

客户端

Linux:

# curl http://ip.wang-li.top:93/4u6385IP

Python

#!/usr/bin/env python3

import requests

def main():
    url = "http://ip.wang-li.top:93/4u6385IP"
    MyIP = requests.get(url).text
    print(MyIP)

if __name__ == '__main__':
    main()

Go

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    url := "http://ip.wang-li.top:93/4u6385IP"

    resp , err := http.Get(url);if err != nil {
        fmt.Println("Http Connect Error", err)
    } else {
        b , err := ioutil.ReadAll(resp.Body); if err != nil {
            fmt.Println("Read Body Error" , err)
        } else {
            fmt.Printf("%s",b)
        }
    }
}

Lua

#!/usr/bin/lua5.3

http = require("socket.http")

function ReturnIP(url)
	local resp = http.request(url)

	return resp
end

print(ReturnIP('http://ip.wang-li.top:93/4u6385IP'))

服务器

Go服务器

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"strings"
	"time"
)

const LoveChina = "富强,民主,文明,和谐。 自由,平等,公正,法治。 爱国,敬业,诚信,友善。"

var (
	h bool
	LitenAddr string
	ListenRoute string
)

func init() {
	flag.BoolVar(&h,"h",false,"This help")
	flag.StringVar(&LitenAddr,"ListenAddr","0.0.0.0:93","Set http server listen address")
	flag.StringVar(&ListenRoute,"ListenRoute","/4u6385IP","Set http server listen Route")
}

func OutIPAddress(w http.ResponseWriter, r *http.Request) {

	if "GET" == r.Method {
		slice01 := strings.Split(r.RemoteAddr,":")
		fmt.Fprintf(w,slice01[0])
		log.Println(time.Now(),"-- 4u6385IP --",r)
		log.Println(time.Now(),"RemoteAdd",slice01[0])
	} else {
		fmt.Fprintf(w,LoveChina)
		log.Println(time.Now() , r.RemoteAddr, LoveChina)
	}
}

func main() {
	flag.Parse()

	if h {
		flag.Usage()
		os.Exit(0)
	}

	if ! strings.HasPrefix(ListenRoute,"/") {
		ListenRoute = "/" + ListenRoute
	}

	http.HandleFunc(ListenRoute,OutIPAddress)

	log.Println("Server running on http://" + LitenAddr + ListenRoute)

	s := &http.Server{
		Addr:	LitenAddr,
		ReadTimeout:10*time.Second,
		WriteTimeout:10*time.Second,
		MaxHeaderBytes:1<<20,
	}
	log.Fatal(s.ListenAndServe())
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!