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 实现:


public class jget {
    public static void main (String[] args) throws IOException {
        try {
            URL url = new URL("");

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

            while ((str = in.readLine()) != null) {

        catch (MalformedURLException e) {}
        catch (IOException e) {}

Python 实现:

import urllib
print urllib.urlopen('').read()

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

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

new URL("").text

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

print __import__('urllib').urlopen('').read()



一条回应 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: Logo

You are commenting using your 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 博主赞过: