Compare commits
1 commit
9e2a7d338d
...
7afcf55d78
Author | SHA1 | Date | |
---|---|---|---|
|
7afcf55d78 |
1 changed files with 30 additions and 0 deletions
30
server.py
30
server.py
|
@ -1,3 +1,4 @@
|
||||||
|
import http.cookies
|
||||||
import http.server
|
import http.server
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
|
@ -11,9 +12,22 @@ class HTTPRequestHandler(http.server.BaseHTTPRequestHandler):
|
||||||
encoded = html.encode('utf-8')
|
encoded = html.encode('utf-8')
|
||||||
length = len(encoded)
|
length = len(encoded)
|
||||||
|
|
||||||
|
sent_cookies = http.cookies.SimpleCookie()
|
||||||
|
sent_cookies['buranun_session'] = 'dihutenosa'
|
||||||
|
#sent_cookies['buranun_session']['domain'] = 'ahti-saarelainen.zgrep.org'
|
||||||
|
sent_cookies['buranun_session']['path'] = '/board'
|
||||||
|
sent_cookies['buranun_session']['max-age'] = 60
|
||||||
|
#sent_cookies['buranun_session']['secure'] = True
|
||||||
|
sent_cookies['buranun_session']['httponly'] = True
|
||||||
|
|
||||||
self.send_response(status_code)
|
self.send_response(status_code)
|
||||||
self.send_header('Content-Type', 'text/html; charset=utf-8')
|
self.send_header('Content-Type', 'text/html; charset=utf-8')
|
||||||
self.send_header('Content-Length', length)
|
self.send_header('Content-Length', length)
|
||||||
|
|
||||||
|
# Since http.cookies doesn't play nicely with http.server we need to do this manually
|
||||||
|
self.flush_headers()
|
||||||
|
self.wfile.write(sent_cookies.output().encode('utf-8') + b'\r\n')
|
||||||
|
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
self.wfile.write(encoded)
|
self.wfile.write(encoded)
|
||||||
|
@ -23,6 +37,22 @@ class HTTPRequestHandler(http.server.BaseHTTPRequestHandler):
|
||||||
self.__send_html(html, status_code = 404)
|
self.__send_html(html, status_code = 404)
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
|
cookies_string = self.headers['cookie']
|
||||||
|
|
||||||
|
if cookies_string is not None:
|
||||||
|
received_cookies = http.cookies.SimpleCookie()
|
||||||
|
|
||||||
|
try:
|
||||||
|
received_cookies.load(cookies_string)
|
||||||
|
except http.cookies.CookieError:
|
||||||
|
print('malformed cookies')
|
||||||
|
|
||||||
|
if 'buranun_session' in received_cookies:
|
||||||
|
print(received_cookies['buranun_session'].value)
|
||||||
|
|
||||||
|
else:
|
||||||
|
print('no cookies')
|
||||||
|
|
||||||
path = urllib.parse.unquote(self.path)
|
path = urllib.parse.unquote(self.path)
|
||||||
|
|
||||||
path_components = [component for component in path.split('/') if component != '']
|
path_components = [component for component in path.split('/') if component != '']
|
||||||
|
|
Loading…
Reference in a new issue