Multilingual programming for retrieving web pages
Tower of Babylon

© Lead Image © Maksym Shevchenko, 123RF.com
We show you how to whip up a script that pulls an HTTP document off the web and how to find out which language offers the easiest approach.
Few programming tasks illuminate the differences between commonly used languages as clearly as that of retrieving a web document. When it comes to shell scripts, admins often turn to the curl
utility, which transfers the data behind a URL without much ado and sends them to the standard output.
But, what if the URL points to a black hole? Or the server denies access? And what if the server returns a redirect? For example, curl http://google.com
does not return the expected HTML page with the search form but just a note that the desired page may be available on www.google.com
. Armed with the -L
option, however, curl
follows the reference and then returns the data from the source it finds there.
What happens with a huge file like a 4K movie containing many gigabytes of data? Will the process exhaust your RAM because it attempts to swallow everything in a single gulp? Does encryption work automatically for an HTTPS URL using the SSL protocol, and does the utility check the server's certificate correctly so that it does not fall victim to a man-in-the-middle attack? Similar to good old curl
, popular programming languages offer all of this, although often only as an add-on package and often requiring quirky approaches.
Go, Hipster Go!
The relatively new Go comes with web support out the box; it is included in the net/http
package with exemplary SSL support. Go programmers are required to handle any errors that occur immediately after function calls and cannot excuse any lapses by claiming that any exceptions that are thrown will be dealt with somewhere down the line, be it as hard-to-read stack traces when the program terminates. This is no doubt a philosophical question with a similar scope to finding your perfect partner or choosing between the vi
and emacs
editors.
Listing 1 [1] shows three different error checks, because various things can go wrong while fetching a document: the act of generating the request (e.g., unsupported protocol), the server returning an error status code (404 for document not found), or an error occurring when retrieving the data via the convoluted pipes of the Internet, which could lead to the data stream terminating abruptly. Functions in Go in general like to return two parameters, a result and an error structure, whose value is set to nil
if everything has gone smoothly.
Listing 1
http-get.go
It is also interesting to note that the net/http
package first executes the request, and then receives the status code of the server in line 14, but still allows some time before scooping off the body data. In fact, the data is grabbed later on by ReadAll()
in line 20, courtesy of the io/ioutil package. The Close()
method on the object body then finally indicates that request processing has finished and that Go can dispose of the data. The related command already can be seen in line 19 but is delayed till the end of the currently executing function by the nifty defer
keyword.
Lean Python
The Python 3 implementation in Listing 2 using the state-of-art requests
library is more compact, because Python throws exceptions that developers can test for later on in a central location. If the request specifies a nonexistent protocol (e.g., abc://
), verification of the server's SSL certificate fails, an I/O error occurs, then an appropriate exception is thrown, which the code either checks separately or in one fell swoop as shown in line 11.
Listing 2
http-get.py
This approach is certainly more convenient, but the typing you saved here can come back like a boomerang later on if an exception is washed up from the depths of the code, like from inside a library developed by someone else. Also the readability suffers because it is not entirely clear which line in the try
block threw the exception. The topic has started already countless, hard-to-settle, and virtually infinite discussions.
It is also funny that r.encoding
states that the page is encoded in ISO-8859-1 after a request for google.de
. Only manual shift to utf-8
in line 7 causes the following r.text
to output the content with UTF-8 encoding. A status code other than 200 does not throw an exception by default and needs to be checked manually. Programmers who like things even more compact can use the raise_for_status()
method to throw an exception if the server reported something other than 200.
Ruby Catchall
Ruby also comes with a built-in HTTP module named net/http
; however, before sending requests, it needs to analyze the URL with another class, URL
. This is far too much work for Ruby-on-Rails fans and prompted the developers to create some Ruby Gems that do the whole thing in a single action. Like Python, Ruby throws exceptions from the depths of the code, and developers can thus deal with errors further upstream.
Listing 3 shows the catchall block, which starts with begin
in line 6, catches exceptions as of rescue
, and finally terminates with end
. The standard library does not follow redirects in its default setting. It interprets the Google.de website as ASCII-8BIT
, which is probably equivalent to the ISO-8859-1 encoding identified by Python in this case.
Listing 3
http-get.rb
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
Red Hat Migrates RHEL from Xorg to Wayland
If you've been wondering when Xorg will finally be a thing of the past, wonder no more, as Red Hat has made it clear.
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.
-
Experimental Wayland Support Planned for Linux Mint 21.3
As with most Linux distributions, the migration to Wayland is in full force. While some distributions have already made the move, Linux Mint has been a bit slower to do so.