Skip to content

Commit 0a75e98

Browse files
life777mikewest
authored andcommitted
Update server file to be used with Python 3 (w3c#36)
1 parent 5120e64 commit 0a75e98

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

demo/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ It consists of
1212
## Setup instructions
1313

1414
1. Download the `demo/` directory.
15-
2. Run `python server.py` in that directory.
15+
2. Run `python server.py` in that directory (Use Python 3.5 or higher).
1616
3. Navigate to `localhost:8000`. (**NOTE:** Clear-Site-Data only works on
1717
`localhost` and `https`. Since `server.py` is HTTP-only, navigating to other
1818
hosts than `localhost` will not work.)

demo/server.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import BaseHTTPServer
1+
import http.server
22
import re
33

4-
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
4+
class RequestHandler(http.server.BaseHTTPRequestHandler):
55
def do_GET(self):
66
# Cache-Control: only-if-cached indicates that the browser should never
77
# fetch the resource over network. Unfortunately, this is not widely
@@ -21,14 +21,14 @@ def do_GET(self):
2121
return
2222

2323
# 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())
2525

2626
def do_POST(self):
2727
# Serve index.html as usual, but with Clear-Site-Data as instructed
2828
# through the POST attributes.
2929

3030
# 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")
3232
# Transformation: ['cookies', 'cache']
3333
datatypes = re.findall('types=([^&]+)', post_data)
3434
# Output: '"cookies","cache"'
@@ -39,9 +39,9 @@ def do_POST(self):
3939
self.send_header('Clear-Site-Data', datatypes)
4040
self.end_headers()
4141

42-
self.wfile.write(file('index.html').read())
42+
self.wfile.write(open('index.html', "r").read().encode())
4343

4444

4545
if __name__ == "__main__":
46-
httpd = BaseHTTPServer.HTTPServer(('', 8000), RequestHandler)
46+
httpd = http.server.HTTPServer(('', 8000), RequestHandler)
4747
httpd.serve_forever()

0 commit comments

Comments
 (0)