Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python One Line HTTP Get

#1
Python One Line HTTP Get

<div><p><em>You may already know about Python’s capability to <a href="https://blog.finxter.com/python-one-liner-webserver/" title="Python One-Liner Webserver HTTP" target="_blank" rel="noreferrer noopener">create a simple web server in a single line</a> 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 <a href="https://pythononeliners.com/" target="_blank" rel="noreferrer noopener" title="https://pythononeliners.com/">Python One-Liners</a> community. Time to change it!</em></p>
<p>This tutorial shows you how to perform simple <strong>HTTP get and post requests</strong> to an existing webserver!</p>
<p><strong>Problem</strong>: 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?</p>
<p><strong>Example</strong>: Say, you want to accomplish the following:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">url = 'https://google.com'
# ... Magic One-Liner Here...
print(result)
# ... Google HTML file: '''
&lt;!doctype html>&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="de">&lt;head>&lt;meta content="text/html; charset=UTF-8" http-equiv="Content-Type">&lt;meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image">&lt;title>Google&lt;/title>... '''</pre>
<p>You can try it yourself in our interactive Python shell:</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/KookyDeepskyblueHardware?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p><em><strong>Exercise</strong>: Does this script download the complete source code of the Google.com website?</em></p>
<p>Let’s learn about the three most important methods to access a website in a single line of Python code—and how they work!</p>
<h2>Method 1: requests.get(url)</h2>
<p>The simplest one-liner solution is the following:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import requests; print(requests.get(url = 'https://google.com').text)</pre>
<p>Here’s how this one-liner works:</p>
<ul>
<li>Import the Python library <a href="https://requests.readthedocs.io/en/master/" target="_blank" rel="noreferrer noopener" title="https://requests.readthedocs.io/en/master/"><code>requests</code> </a>that handles the details of requesting the websites from the server in an easy-to-process format.</li>
<li>Use the <code>requests.get(...)</code> method to access the website and pass the URL <code>'https://google.com'</code> as an argument so that the function knows which location to access.</li>
<li>Access the actual body of the get <code>request</code> (the return value is a request object that also contains some useful meta information like the file type, etc.).</li>
<li><a href="https://blog.finxter.com/the-separator-and-end-arguments-of-the-python-print-function/" target="_blank" rel="noreferrer noopener" title="Python Print Function [And Its SECRET Separator &amp; End Arguments]">Print </a>the result to the shell.</li>
</ul>
<p>Note that the semicolon is used to <a href="https://blog.finxter.com/how-to-write-multiple-statements-on-a-single-line-in-python/" target="_blank" rel="noreferrer noopener" title="How to Write Multiple Statements on a Single Line in Python?">one-linerize this method</a>. This is useful if you want to run this command from your operating system with the following command:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">python -r "import requests; print(requests.get(url = 'https://google.com').text)"</pre>
<p>The output is the desired Google website:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">'''
&lt;!doctype html>&lt;html itemscope="" itemtype="http://schema.org/WebPage" lang="de">&lt;head>&lt;meta content="text/html; charset=UTF-8" http-equiv="Content-Type">&lt;meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image">&lt;title>Google&lt;/title>... '''</pre>
<p>Note that you may have to install the requests library with the following command in your operating system terminal:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install requests</pre>
<p>A similar approach can be taken if you want to issue a post request:</p>
<h2>Method 2: requests.post(url)</h2>
<p>What if you want to post some data to a web resource? Use the post method of the <code>requests</code> module! Here’s a minimal one-liner example of the <code>request.post()</code> method:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import requests as r; print(r.post('https://example.com', {'key': 'val'}).text)</pre>
<p>The approach is similar to the first one:</p>
<ul>
<li>Import the <code>requests</code> module.</li>
<li>Call the <code>r.post(...)</code> method.</li>
<li>Pass the URL <code>'https://example.com'</code> as the first parameter into the function.</li>
<li>Pass the value to be posted to the URL—in our case a simple key-value pair in a <a href="https://blog.finxter.com/python-dictionary/" title="Python Dictionary – The Ultimate Guide" target="_blank" rel="noreferrer noopener">dictionary </a>data structure. </li>
<li>Access the body via the <code>text</code> attribute of the <code>request</code> object. </li>
<li>Print it to the shell.</li>
</ul>
<h2>Method 3: urllib.request</h2>
<p>A recommended way to <a href="https://docs.python.org/3/howto/urllib2.html" target="_blank" rel="noreferrer noopener" title="https://docs.python.org/3/howto/urllib2.html">fetch web resources</a> from a website is the <code>urllib.request()</code> function. This also works to create a simple one-liner to access the Google website in Python 3 as before:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import urllib.request as r; print(r.urlopen('https://google.com').read())</pre>
<p>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:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">python -r "import urllib.request as r; print(r.urlopen('https://google.com').read())"</pre>
<p>Congrats! You now have mastered the art of accessing websites in a<a href="https://blog.finxter.com/python-one-line-x/" target="_blank" rel="noreferrer noopener" title="Python One Line X"> single line of Python code</a>. If you’re interested in boosting your one-liner power, have a look at my new book:</p>
<h2>Python One-Liners Book</h2>
<p><strong>Python programmers will improve their computer science skills with these useful one-liners.</strong></p>
<figure class="wp-block-image size-medium is-resized"><a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noopener noreferrer"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-1024x944.jpg" alt="Python One-Liners" class="wp-image-10007" width="512" height="472" srcset="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x277.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x708.jpg 768w" sizes="(max-width: 512px) 100vw, 512px" /></a></figure>
<p><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener" title="https://amzn.to/2WAYeJE"><em>Python One-Liners</em> </a>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.</p>
<p>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:</p>
<p><strong>•</strong>&nbsp;&nbsp;Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution<br /><strong>•</strong>&nbsp;&nbsp;Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics<br /><strong>•</strong>&nbsp;&nbsp;Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning<br /><strong>•</strong>&nbsp;&nbsp;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<br /><strong>•</strong>&nbsp;&nbsp;Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting</p>
<p>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.</p>
<p><strong><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener" title="https://amzn.to/2WAYeJE"><em>Get your Python One-Liners Now!!</em></a></strong></p>
</div>


https://www.sickgaming.net/blog/2020/09/...-http-get/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016