~ozzloy/emacs-bug-63941

emacs-bug-63941/server.py -rwxr-xr-x 2.5 KiB
25819f00Daniel Watson 7 months ago
                                                                                
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
#! /usr/bin/env python
import cgi
import http.server
import socketserver
import socket


ab = '<input name="a" value="b" />'
f = '<input name="e" type="file" />'
f2 = '<input name="g" type="file" />'
submit_name = '<input name="submit" />'
submit = '<input type="submit" />'
template = '''\
<html>
<body>
<form action="{}" enctype="multipart/form-data" method="post">
    {}
</form>
</body>
</html>
'''
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_POST(self):
        form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={
                'REQUEST_METHOD':'POST',
                'CONTENT_TYPE'  :self.headers['Content-Type']
            }
        )

        print(f'{form = }')

        self.route()

    def do_GET(self):
        self.route()

    def route(self):
        match self.path:
            case '/f':
                self.f()
            case '/':
                self.index()
            case '/f_f':
                self.f_f()
            case '/f_ab':
                self.f_ab()
            case '/ab_f':
                self.ab_f()
            case '/f_submit':
                self.f_submit()
            case '/submit':
                self.submit()

    def submit(self):
        # page with just a submit button
        self.page('')

    def f_submit(self):
        self.page(f + submit_name)

    def f(self):
        self.page(f)

    def index(self):
        self.f()

    def f_f(self):
        self.page(f + f2)

    def ab_f(self):
        self.page(ab + f)

    def f_ab(self):
        self.page(f + ab)

    def page(self, content):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        page = (
            template
            .format(self.path, content + submit)
            .encode('utf-8')
        )
        self.wfile.write(page)
        self.wfile.flush()

if __name__ == '__main__':
    port = 8085
    with socketserver.TCPServer(("", port), MyHTTPRequestHandler) as httpd:
        print('if you haven\'t already, open another terminal,')
        print('    socat -v tcp-listen:8086,fork tcp:localhost:8085')
        print('then use EWW to browse to localhost:8086 and upload "filename"')
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass
        print("closing server")
        httpd.socket.shutdown(socket.SHUT_RDWR)
        httpd.socket.close()
        httpd.server_close()
        print("closed server")