Gufo HTTP Example: Get Request
Get
is one of the basic HTTP operations allowing to download
single page.
get.py import sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
main ( sys . argv [ 1 ])
Let's see the details.
get.py import sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
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 sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
main ( sys . argv [ 1 ])
HttpClient
object holds all necessary API. We're using a synchronous
version from gufo.http.sync_client
.
get.py import sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
main ( sys . argv [ 1 ])
We define our main function and expect resource URL.
get.py import sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
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 context manager
using the 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 sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
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.
get.py import sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
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 sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
main ( sys . argv [ 1 ])
If status code is not OK, we print and error message and terminate our function.
get.py import sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
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 sys
from gufo.http.sync_client import HttpClient
def main ( url : str ) -> None :
with HttpClient () as client :
r = client . get ( url )
if r . status != 200 :
print ( f "Invalid response code: { r . status } " )
return
print ( r . content . decode ())
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/sync/get.py https://gufolabs.com/
<!DOCTYPE html>
<html>
...