File: urlrequest.py

package info (click to toggle)
kivy 2.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,316 kB
  • sloc: python: 80,678; ansic: 5,326; javascript: 780; objc: 725; lisp: 195; sh: 173; makefile: 150
file content (128 lines) | stat: -rw-r--r-- 3,465 bytes parent folder | download | duplicates (3)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from kivy.lang import Builder
from kivy.app import App
from kivy.network.urlrequest import UrlRequest
from kivy.properties import NumericProperty, StringProperty, DictProperty

import json


KV = '''
#:import json json
#:import C kivy.utils.get_color_from_hex

BoxLayout:
    orientation: 'vertical'
    Label:
        text: 'see https://httpbin.org for more information'

    TextInput:
        id: ti
        hint_text: 'type url or select from dropdown'
        size_hint_y: None
        height: 48
        multiline: False
        foreground_color:
            (
            C('000000')
            if (self.text).startswith('http') else
            C('FF2222')
            )

    BoxLayout:
        size_hint_y: None
        height: 48
        Spinner:
            id: spinner
            text: 'select'
            values:
                [
                'http://httpbin.org/ip',
                'http://httpbin.org/user-agent',
                'http://httpbin.org/headers',
                'http://httpbin.org/delay/3',
                'http://httpbin.org/image/jpeg',
                'http://httpbin.org/image/png',
                'https://httpbin.org/delay/3',
                'https://httpbin.org/image/jpeg',
                'https://httpbin.org/image/png',
                ]
            on_text: ti.text = self.text

        Button:
            text: 'GET'
            on_press: app.fetch_content(ti.text)
            disabled: not (ti.text).startswith('http')
            size_hint_x: None
            width: 50

    Label:
        text: str(app.status)

    TextInput:
        readonly: True
        text: app.result_text

    Image:
        source: app.result_image
        nocache: True

    TextInput
        readonly: True
        text: json.dumps(app.headers, indent=2)
'''


class UrlExample(App):
    status = NumericProperty()
    result_text = StringProperty()
    result_image = StringProperty()
    headers = DictProperty()

    def build(self):
        return Builder.load_string(KV)

    def fetch_content(self, url):
        self.cleanup()
        UrlRequest(
            url,
            on_success=self.on_success,
            on_failure=self.on_failure,
            on_error=self.on_error
        )

    def cleanup(self):
        self.result_text = ''
        self.result_image = ''
        self.status = 0
        self.headers = {}

    def on_success(self, req, result):
        self.cleanup()
        headers = req.resp_headers
        content_type = headers.get('content-type', headers.get('Content-Type'))
        if content_type.startswith('image/'):
            fn = 'tmpfile.{}'.format(content_type.split('/')[1])
            with open(fn, 'wb') as f:
                f.write(result)
            self.result_image = fn
        else:
            if isinstance(result, dict):
                self.result_text = json.dumps(result, indent=2)
            else:
                self.result_text = result
        self.status = req.resp_status
        self.headers = headers

    def on_failure(self, req, result):
        self.cleanup()
        self.result_text = result
        self.status = req.resp_status
        self.headers = req.resp_headers

    def on_error(self, req, result):
        self.cleanup()
        self.result_text = str(result)


if __name__ == '__main__':
    UrlExample().run()