File: form.cgi

package info (click to toggle)
paste 3.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,472 kB
  • sloc: python: 19,960; javascript: 8,028; makefile: 47; sh: 24
file content (68 lines) | stat: -rwxr-xr-x 1,704 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

print('Content-type: text/plain')
print('')

import sys
import warnings
from os.path import dirname

base_dir = dirname(dirname(dirname(__file__)))
sys.path.insert(0, base_dir)

with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=DeprecationWarning)
    try:
        import pkg_resources
    except ImportError:
        # Ignore
        pass

from paste.util.field_storage import FieldStorage

class FormFieldStorage(FieldStorage):

    def _key_candidates(self, key):
        yield key

        try:
            # assume bytes, coerce to str
            try:
                yield key.decode(self.encoding)
            except UnicodeDecodeError:
                pass
        except AttributeError:
            # assume str, coerce to bytes
            try:
                yield key.encode(self.encoding)
            except UnicodeEncodeError:
                pass

    def __getitem__(self, key):
        error = None

        for candidate in self._key_candidates(key):
            if isinstance(candidate, bytes):
                # ouch
                candidate = repr(candidate)
            try:
                return super().__getitem__(candidate)
            except KeyError as e:
                if error is None:
                    error = e

        # fall through, re-raise the first KeyError
        raise error

    def __contains__(self, key):
        for candidate in self._key_candidates(key):
            if super().__contains__(candidate):
                return True
        return False


form = FormFieldStorage()

print('Filename:', form['up'].filename)
print('Name:', form['name'].value)
print('Content:', form['up'].file.read())