Python One Line HTTP Get - Be on the Right Side of Change (original) (raw)

You may already know about Python’s capability to create a simple web server in a single line of Python code. Old news. Besides, what’s the point of creating a webserver that only runs on your machine? It would be far more interesting to learn how to access existing websites in a single line of code. Surprisingly, nobody talks about this in the Python One-Liners community. Time to change it!

This tutorial shows you how to perform simple HTTP get and post requests to an existing webserver!

Problem: Given the URL location of a webserver serving websites via HTTP. How to access the webserver’s response in a single line of Python code?

Example: Say, you want to accomplish the following:

url = 'https://google.com'

... Magic One-Liner Here...

print(result)

... Google HTML file:

''' Google... '''

You can try it yourself in our interactive Python shell:

Exercise: Does this script download the complete source code of the Google.com website?

Let’s learn about the three most important methods to access a website in a single line of Python code—and how they work!

Method 1: requests.get(url)

The simplest one-liner solution is the following:

import requests; print(requests.get(url = 'https://google.com').text)

Here’s how this one-liner works:

Note that the semicolon is used to one-linerize this method. This is useful if you want to run this command from your operating system with the following command:

python -r "import requests; print(requests.get(url = 'https://google.com').text)"

The output is the desired Google website:

''' Google... '''

Note that you may have to install the requests library with the following command in your operating system terminal:

pip install requests

A similar approach can be taken if you want to issue a post request:

Method 2: requests.post(url)

What if you want to post some data to a web resource? Use the post method of the requests module! Here’s a minimal one-liner example of the request.post() method:

import requests as r; print(r.post('https://example.com', {'key': 'val'}).text)

The approach is similar to the first one:

👉 Recommended: I Used This Python Script to Check a Website Every X Seconds for a Given Word

Method 3: urllib.request

A recommended way to fetch web resources from a website is the urllib.request() function. This also works to create a simple one-liner to access the Google website in Python 3 as before:

import urllib.request as r; print(r.urlopen('https://google.com').read())

It works similarly than before by returning a Request object that can be accessed to read the server’s response. We’re cramming everything into a single line so that you can run it from your OS’s terminal:

python -r "import urllib.request as r; print(r.urlopen('https://google.com').read())"

Congrats! You now have mastered the art of accessing websites in a single line of Python code. If you’re interested in boosting your one-liner power, have a look at my new book:

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!