1
- import BaseHTTPServer
1
+ import http . server
2
2
import re
3
3
4
- class RequestHandler (BaseHTTPServer .BaseHTTPRequestHandler ):
4
+ class RequestHandler (http . server .BaseHTTPRequestHandler ):
5
5
def do_GET (self ):
6
6
# Cache-Control: only-if-cached indicates that the browser should never
7
7
# fetch the resource over network. Unfortunately, this is not widely
@@ -21,14 +21,14 @@ def do_GET(self):
21
21
return
22
22
23
23
# Otherwise, just serve index.html, as that is the only page we have.
24
- self .wfile .write (file ('index.html' ).read ())
24
+ self .wfile .write (open ('index.html' , "r" ).read (). encode ())
25
25
26
26
def do_POST (self ):
27
27
# Serve index.html as usual, but with Clear-Site-Data as instructed
28
28
# through the POST attributes.
29
29
30
30
# Input: "types=cookies&types=cache"
31
- post_data = self .rfile .read (int (self .headers ['Content-Length' ]))
31
+ post_data = self .rfile .read (int (self .headers ['Content-Length' ])). decode ( "utf-8" )
32
32
# Transformation: ['cookies', 'cache']
33
33
datatypes = re .findall ('types=([^&]+)' , post_data )
34
34
# Output: '"cookies","cache"'
@@ -39,9 +39,9 @@ def do_POST(self):
39
39
self .send_header ('Clear-Site-Data' , datatypes )
40
40
self .end_headers ()
41
41
42
- self .wfile .write (file ('index.html' ).read ())
42
+ self .wfile .write (open ('index.html' , "r" ).read (). encode ())
43
43
44
44
45
45
if __name__ == "__main__" :
46
- httpd = BaseHTTPServer .HTTPServer (('' , 8000 ), RequestHandler )
46
+ httpd = http . server .HTTPServer (('' , 8000 ), RequestHandler )
47
47
httpd .serve_forever ()
0 commit comments