the5fire

关注Python、Django、Vim、Linux、Web开发、团队管理和互联网--Life is short, we need Python.


个人电脑做服务器(二)

作者:the5fire | 标签:       | 发布:2011-04-01 9:29 p.m. | 阅读量: 8661, 8333

铁通用户获取准确外网ip


  本来通过花生壳可以实现外网访问我电脑上的网站,但是就像上篇文章中所说,在花生壳上需要选择服务器线路,然后铁通的线路只能试用一天,想要再次使用的话,你懂的(囧)
  所以又下载了另外一个动态域名映射工具:易联:

  这个工具最大的优点就是支持自定义的外网ip获取来源,如图:

  这个是我本地的一个jsp网站,我在网站首页写了一段代码通过ip138获取到自己的外网ip,然后输出。这样易联软件就可以获得我外网的ip进行域名映射了。
  Jsp代码如下:


<%@ page import="java.io.*" contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.net.*,java.util.regex.*"%>
<%
/**
* code by the5fire
* blog:http://www.the5fire.com
**/
String htmpath=null;
BufferedReader in = null;
InputStreamReader isr = null;
InputStream is = null;
HttpURLConnection huc = null;
String ipFilter = "\\d+\\.\\d+\\.\\d+\\.\\d+";
String host = "";
try{
URL url = new URL("http://www.ip138.com/ip2city.asp"); //创建 URL
huc = (HttpURLConnection)url.openConnection();
is = huc.getInputStream();
isr = new InputStreamReader(is);
in = new BufferedReader(isr);
String line = null;
Pattern p = Pattern.compile(ipFilter);
while(((line = in.readLine()) != null)) {
Matcher m = p.matcher(line);
if (m.find()) {
host = m.group();//IP地址
}
if(line.length()==0)
continue;
}
}
catch (Exception e) {
System.err.println(e);
}
finally { //无论如何都要关闭流
try { is.close(); isr.close();in.close();huc.disconnect();
} catch (Exception e) {}
}
%>
<%=host %>

  不过令我比较郁闷的是,目前我自己可以通过外网ip访问,但是别人好像不行,目前原因不明,待深究。哪位大侠要是知道不妨告知一声。


- from the5fire.com
----EOF-----

微信公众号:Python程序员杂谈


其他分类: