Skip to content

Commit

Permalink
Update server file to be used with Python 3 (w3c#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
life777 authored and mikewest committed Oct 31, 2018
1 parent 5120e64 commit 0a75e98
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ It consists of
## Setup instructions

1. Download the `demo/` directory.
2. Run `python server.py` in that directory.
2. Run `python server.py` in that directory (Use Python 3.5 or higher).
3. Navigate to `localhost:8000`. (**NOTE:** Clear-Site-Data only works on
`localhost` and `https`. Since `server.py` is HTTP-only, navigating to other
hosts than `localhost` will not work.)
Expand Down
12 changes: 6 additions & 6 deletions demo/server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BaseHTTPServer
import http.server
import re

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

# Otherwise, just serve index.html, as that is the only page we have.
self.wfile.write(file('index.html').read())
self.wfile.write(open('index.html', "r").read().encode())

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

# Input: "types=cookies&types=cache"
post_data = self.rfile.read(int(self.headers['Content-Length']))
post_data = self.rfile.read(int(self.headers['Content-Length'])).decode("utf-8")
# Transformation: ['cookies', 'cache']
datatypes = re.findall('types=([^&]+)', post_data)
# Output: '"cookies","cache"'
Expand All @@ -39,9 +39,9 @@ def do_POST(self):
self.send_header('Clear-Site-Data', datatypes)
self.end_headers()

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


if __name__ == "__main__":
httpd = BaseHTTPServer.HTTPServer(('', 8000), RequestHandler)
httpd = http.server.HTTPServer(('', 8000), RequestHandler)
httpd.serve_forever()

0 comments on commit 0a75e98

Please sign in to comment.