如何获取公网ip,自己搭建公网ip服务器的方法


#Go语言初体验 – 获取公网IP小服务。由于免费的动态dns刷新IP很慢, 手动实时获取办公室网络公网IP。

#GetIp.go 是服务端程序, 用于返回当前访问的请求的公网IP。 demo地址: http://47.52.66.195:56667/

#安装go环境

yum install -y go

#下载源码包

wget https://github.com/liuhu/PublicIpServer/raw/master/GetIp.go

#运行脚本

go run Getip.go &

评论说说大家最喜欢的语音

go ? python? java? php ?或者什么?

源代码如下

cat GetIp.go

package main

import (

“fmt”

“net/http”

“log”

“net”

)

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

// get client ip address

ip,_,_ := net.SplitHostPort(r.RemoteAddr)

// print out the ip address

fmt.Fprintf(w,ip)

}

func main() {

http.HandleFunc(“/”, getRemoteIp) //设置访问的路由

err := http.ListenAndServe(“:56667”, nil) //设置监听的端口

if err != nil {

log.Fatal(“Server ERROR: “, err)

}

}

用Go语言写一个获取公网ip的接口

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论