![]() |
|
[Tut] Python One Line HTTP Get - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python One Line HTTP Get (/thread-97184.html) |
[Tut] Python One Line HTTP Get - xSicKxBot - 09-11-2020 Python One Line HTTP Get 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: Code: url = 'https://google.com'
# ... Magic One-Liner Here...
print(result)
# ... Google HTML file: '''
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="de"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title>... '''You can try it yourself in our interactive Python shell: https://repl.it/@finxter/KookyDeepskyblueHardware?lite=true 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: Code: 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: Code: python -r "import requests; print(requests.get(url = 'https://google.com').text)"The output is the desired Google website: Code: '''
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="de"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title>... '''Note that you may have to install the requests library with the following command in your operating system terminal: Code: pip install requestsA 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 Code: requestsCode: request.post()Code: import requests as r; print(r.post('https://example.com', {'key': 'val'}).text)The approach is similar to the first one:
Method 3: urllib.request A recommended way to fetch web resources from a website is the Code: urllib.request()Code: 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: Code: 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 asingle 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 Python programmers will improve their computer science skills with these useful one-liners. ![]() Python One-Linerswill 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 tips and tricks, regular expressions, machine learning, core data science topics, and 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: • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting 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 Now!! |