File: async_http.py

package info (click to toggle)
kore 4.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,912 kB
  • sloc: ansic: 29,227; makefile: 344; sh: 278; python: 149; cpp: 34
file content (49 lines) | stat: -rw-r--r-- 1,715 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#
# Copyright (c) 2019 Joris Vink <joris@coders.se>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

#
# Asynchronous HTTP client example.
#

import kore

# Handler called for /httpclient
@kore.route("/httpclient", methods=["get"])
async def httpclient(req):
    # Create an httpclient.
    client = kore.httpclient("https://kore.io")

    # Do a simple GET request.
    print("firing off request")
    status, body = await client.get()
    print("status: %d, body: '%s'" % (status, body))

    # Reuse and perform another GET request, returning headers too this time.
    status, headers, body = await client.get(return_headers=True)
    print("status: %d, headers: '%s'" % (status, headers))

    # What happens if we post something?
    status, body = await client.post(body=b"hello world")
    print("status: %d, body: '%s'" % (status, body))

    # Add some custom headers to our requests.
    status, body = await client.get(
        headers={
            "x-my-header": "async-http"
        }
    )

    req.response(200, b'async done')