Skip to content

Get Method

Iury O. G. Figueiredo edited this page Dec 4, 2020 · 2 revisions

The HTTP GET request is performed by using the class Get from websnake module.

from websnake import Get, ResponseHandle, core, die

def handle_response(request, response):
    print('Headers:', response.headers)
    print('Code:', response.code)
    print('Version:', response.version)
    print('Reason:', response.reason) 

if __name__ == '__main__':
    request = Get('https://facebook.com/')
    
    request.add_map(ResponseHandle.RESPONSE, handle_response)
    request.add_map(ResponseHandle.DONE, lambda req, resp: die('Bye!'))

    core.gear.mainloop()

The statement below is responsible for importing basic tools to be used.

from websnake import Get, ResponseHandle, core, die

You will be importing ResponseHandle, core and die probably in all your Websnake applications. The ResponseHandle holds three important attributes.

These attributes are important events that are related to a given request connection.

The event below is a class and its purpose is to be a mean for users to have callbacks called when the server replies with a response.

Notice that when a given request is made then ResponseHandle.RESPONSE might be fired multiple times depending on the number of redirects.

ResponseHandle.RESPONSE

The previous event carries two arguments (request, response) thus a handle for that event would be like.

def handle_response(request, response):
    pass

The event below happens when it has already handled redirects when it exists and the response content is in fact the one requested.

ResponseHandle.DONE

As ResponseHandle.RESPONSE it carries two arguments (request, response) thus a handle for such an event could be.

def handle_done(request, response):
    pass