Saturday, June 5, 2010

Java vs. Python: fetching URLs

Here we go, another Java vs. Python comparison (I just can't help myself). This time it's about standard library usefulness in doing certain tasks. Fetching the contents of a URL should be a trivial one, but in Java, it's not. Especially if the contents of that URL are gzipped and use a nice charset such as UTF-8.

Java:

URL url = new URL("http://www.example.com/"); URLConnection conn = url.openConnection(); conn.connect(); InputStream in; if (conn.getContentEncoding().equals("gzip")) { in = new GZIPInputStream(conn.getInputStream()); } else { in = conn.getInputStream(); } String charset = conn.getContentType(); BufferedReader reader; if (charset.indexOf("charset=") != -1) { charset = charset.substring(charset.indexOf("charset=") + 8); reader = new BufferedReader(new InputStreamReader(in, charset)); } else { charset = null; reader = new BufferedReader(new InputStreamReader(in)); } StringBuilder builder = new StringBuilder(); String line = reader.readLine(); while (line != null) { builder.append(line + '\n'); line = reader.readLine(); } String content = builder.toString(); // FINALLY!

Python:

content = urllib2.urlopen("http://www.example.com/").read()

At first I though yeah, well, Java is probably older and wasn't designed to do such things very often. I was wrong. Java appeared in 1995, Python in 1991.

5 comments:

  1. In retrospect, Java was heavy in implementation from other languages and Python was probably wasn't too mature for such a vast library. You can't treat programming languages like children who study right when they're born. I doubt python at the time had a function for fetching URL content, let alone popular usage of the HTTP header content type gzip.

    ReplyDelete
  2. I would think that the underlying python code does something similar. I could easily wrap the java code in a function.

    ReplyDelete
  3. In this fashion my friend Wesley Virgin's adventure launches with this SHOCKING AND CONTROVERSIAL video.

    You see, Wesley was in the military-and soon after leaving-he discovered hidden, "MIND CONTROL" tactics that the government and others used to get everything they want.

    THESE are the exact same methods lots of famous people (notably those who "come out of nowhere") and elite business people used to become rich and famous.

    You probably know that you use less than 10% of your brain.

    That's really because most of your BRAINPOWER is UNCONSCIOUS.

    Perhaps that expression has even occurred IN YOUR own head... as it did in my good friend Wesley Virgin's head about 7 years ago, while riding an unregistered, beat-up bucket of a car without a license and $3.20 on his banking card.

    "I'm very frustrated with going through life paycheck to paycheck! When will I become successful?"

    You've been a part of those those types of conversations, right?

    Your own success story is going to happen. You just have to take a leap of faith in YOURSELF.

    Watch Wesley Virgin's Video Now!

    ReplyDelete
  4. If you're trying to lose weight then you absolutely need to start following this totally brand new personalized keto plan.

    To design this service, licensed nutritionists, fitness couches, and professional chefs united to develop keto meal plans that are useful, convenient, price-efficient, and fun.

    From their launch in early 2019, 100's of people have already transformed their body and well-being with the benefits a good keto plan can provide.

    Speaking of benefits: in this link, you'll discover 8 scientifically-certified ones provided by the keto plan.

    ReplyDelete
  5. Compre documentos en línea, documentos originales y registrados.
    Acerca de Permisodeespana, algunos dicen que somos los solucionadores de problemas, mientras que otros se refieren a nosotros como vendedores de soluciones. Contamos con cientos de clientes satisfechos a nivel mundial. Hacemos documentos falsos autorizados y aprobados como Permiso de Residencia Español, DNI, Pasaporte Español y Licencia de Conducir Española. Somos los fabricantes y proveedores de primer nivel de estos documentos, reconocidos a nivel mundial.

    Comprar permiso de residencia,
    permiso de residenciareal y falso en línea,
    Compre licencia de conducir en línea,
    Compre una licencia de conducir española falsa en línea,
    Comprar tarjeta de identificación,
    Licencia de conducir real y falsa,
    Compre pasaporte real en línea,

    Visit Here fpr more information. :- https://permisodeespana.com/licencia-de-conducir-espanola/
    Address: 56 Guild Street, London, EC4A 3WU (UK)
    Email: contact@permisodeespana.com
    WhatsApp: +443455280186

    ReplyDelete