def wrap(*args, **kwargs): session_key = request.cookies.get(SESSION_COOKIE) if not session_key: session_key = str(uuid.uuid4()) session_store = SessionStore() session = session_store.get_session(session_key) request.session = session response = f(*args, **kwargs) response.set_cookie(SESSION_COOKIE, session_key) session_store.set_session(session_key, session) print(session) return response
from http.server import BaseHTTPRequestHandler, HTTPServer from http import cookies from urllib.parse import urlparse, parse_qs import json from passlib.hash import pbkdf2_sha256 import sys from democracy_db import DemocracyDB from user_db import UserDB from session_store import SessionStore gSessionStore = SessionStore() class MyRequestHandler(BaseHTTPRequestHandler): def end_headers(self): self.sendCookie() # (access control allow origin, *) isn't allowed with (...-credentials, true) self.send_header("Access-Control-Allow-Origin", self.headers["Origin"]) print("self.headers[\"Origin\"] -> ", self.headers["Origin"]) self.send_header("Access-Control-Allow-Credentials", "true") BaseHTTPRequestHandler.end_headers(self) def load_cookie(self): if "Cookie" in self.headers: print("self.headers['Cookie'] -> ", self.headers["Cookie"]) self.cookie = cookies.SimpleCookie(self.headers["Cookie"]) else: self.cookie = cookies.SimpleCookie() print("self.cookie -> ", self.cookie) def sendCookie(self):
# Import the folowwing classes from http.server import BaseHTTPRequestHandler, HTTPServer import json import sys from sql_database import professorsDB from urllib.parse import parse_qs from http import cookies from passlib.hash import bcrypt from session_store import SessionStore SESSION_STORE = SessionStore() class MyrequestHandler(BaseHTTPRequestHandler): def LoginSession(self): if "userId" in self.session: return True else: return False # Function for send and end headers def end_headers(self): self.send_cookie() self.send_header("Access-Control-Allow-Origin", self.headers["origin"]) self.send_header("Access-Control-Allow-Credentials", "true") BaseHTTPRequestHandler.end_headers(self) def load_cookie(self): if "Cookie" in self.headers: print("Cookie") self.cookie = cookies.SimpleCookie(self.headers["Cookie"])
from http.cookies import SimpleCookie from http.server import BaseHTTPRequestHandler, HTTPServer from urllib.parse import parse_qs import json import sys from passlib.hash import bcrypt from http import cookies # Fix hanging from socketserver import ThreadingMixIn from garden_db import GardensDB from session_store import SessionStore STORE = SessionStore() class GardensHTTPRequestHandler(BaseHTTPRequestHandler): """ The HTTP request handler for the gardens app. """ # HTTP METHODS def do_OPTIONS(self): """ Handle OPTIONS requests. Specififes allowed methods/headers. """ self.load_session() self.send_response(200) self.send_header('Access-Control-Allow-Credentials', 'true') # For multiple domains, echo back the self.headers['Origin'] self.send_header("Access-Control-Allow-Origin", self.headers["Origin"]) self.send_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
from http.server import BaseHTTPRequestHandler, HTTPServer from urllib.parse import parse_qs import json from http import cookies from passlib.hash import bcrypt import sys from session_store import SessionStore from nba_db import NBADB from users_db import USERDB gSessionStore = SessionStore( ) # has to be global because the rest of the class will be loaded each time #this class is used once per request class MyRequestHandler(BaseHTTPRequestHandler): ################ COOKIES AND SESSIONS ####################### def loadCookie(self): # loads the cookie that the client sent # always loads the cookie first, even if it isn't new; that way we can send it back to client in sendCookie if "Cookie" in self.headers: self.cookie = cookies.SimpleCookie(self.headers["Cookie"]) else: #if there isn't a cookie, create one self.cookie = cookies.SimpleCookie() return
#!/usr/bin/env python3 #built in http stuff from http.server import BaseHTTPRequestHandler, HTTPServer from urllib.parse import parse_qs import json from passlib.hash import bcrypt from http import cookies #from os import curdir, sep from namesDB import NamesDB from session_store import SessionStore SESSION_STORE = SessionStore() # has to outlive a request so it's global class MyRequestHandler(BaseHTTPRequestHandler): def end_headers(self): self.send_cookie() self.send_header("Access-Control-Allow-Origin", self.headers["Origin"]) self.send_header("Access-Control-Allow-Credentials", "true") BaseHTTPRequestHandler.end_headers(self) def load_cookie(self): if "Cookie" in self.headers: self.cookie = cookies.SimpleCookie(self.headers["Cookie"]) else: self.cookie = cookies.SimpleCookie() def send_cookie(self): for morsel in self.cookie.values(): self.send_header("Set-Cookie", morsel.OutputString())