Gufo HTTP Example: Get Request
Get
is one of the basic HTTP operations allowing to download
single page. We have mastered how to process synchronous get
request in our get.py example. The asynchronous
implementation is only slightly more complicated.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
Let's see the details.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
To run async function from our synchronous script,
so we need to import asyncio
to use asyncio.run()
.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
Import sys
module to parse the CLI argument.
Warning
We use sys.argv
only for demonstration purposes. Use argsparse
or alternatives
in real-world applications.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
HttpClient
object holds all necessary API. We're using an asynchronous
version from gufo.http.async_client
.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
We define our asychronous main function and expect resource URL.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
First, we need to create HttpClient
object which wraps the client's session.
The HttpClient
may be used as an instance directly or operated as asynchronous context manager
using the async with
clause. When used as a context manager,
the client automatically closes all connections on the exit of context,
so its lifetime is defined explicitly.
HttpClient
constructor offers lots of configuration variables for fine-tuning. Refer to the
HttpClient reference
for further details. In our example we use default settings.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
.get()
method starts a HTTP GET request and await for response headers.
It assepts an url as mandatary argument and returns a SyncResponse
instance.
The function is asyncronous and needs to be awaited.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
We can inspect response code and headers without reading whole response body.
For our example we just check the status code is 200 OK.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
If status code is not OK, we print and error message and terminate our function.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
The .content
attribute of Response
contains the response
body. This attribute has type of bytes
,
so we convert it into str
using .decode()
method. In our example
we consider the response is encoding using UTF-8 encoding.
Then we print decoded result.
get.py import asyncio
import sys
from gufo.http.async_client import HttpClient
async def main ( url : str ) -> None :
async with HttpClient () as client :
r = await client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
asyncio . run ( main ( sys . argv [ 1 ]))
Lets run our main()
function pass first command-line parameter as url.
Running
Let's check our script. Run example as:
$ python3 examples/async/get.py https://gufolabs.com/
<!DOCTYPE html>
<html>
...