Ross Wan's World!

Python, Ajax, PHP and Linux.

Python vs. Java: HTTP GET Request

Posted by Ross Wan 于 2008/09/12

这是网上的一篇 blog,它列出了 HTTP GET 的 Java 和 Python 实现代码。

Java 实现:

import java.net.*;
import java.io.*;

public class jget {
    public static void main (String[] args) throws IOException {
        try {
            URL url = new URL("http://www.google.com");

            BufferedReader in =
                new BufferedReader(new InputStreamReader(url.openStream()));
            String str;

            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }

            in.close();
        }
        catch (MalformedURLException e) {}
        catch (IOException e) {}
    }
}

Python 实现:

import urllib
print urllib.urlopen('http://www.google.com').read()

Python 的实现代码比 Java 简单很多很多,这得益于 Python 内建的库。不过,这里并不是比较两种语言的优劣。主要是想看看,究竟要实现 HTTP GET,可以多简单?!看看下面其他人的代码。

Groovy(基于 JVM 的脚本语言):

new URL("http://google.com").text

下面还是 Python 的,是上面 Python 代码的简化版:

print __import__('urllib').urlopen('http://www.google.com').read()

参考资料:

Advertisements

一条回应 to “Python vs. Java: HTTP GET Request”

  1. Hello! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest writing
    a blog article or vice-versa? My site goes over a lot of
    the same subjects as yours and I feel we could greatly benefit from
    each other. If you’re interested feel free
    to send me an email. I look forward to hearing from you!
    Great blog by the way!

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

 
%d 博主赞过: