def do_GET(self): # super().handle() cookies = None print(self.headers) if 'Cookie' in self.headers: cookies = http.cookies.BaseCookie(self.headers['Cookie']) print(cookies) print(cookies.keys()) for k, v in cookies.items(): print(k, ":", v) self.send_response(200) # 响应行 self.send_header('Content-type', 'text/html;charset=utf-8') # 响应头 self.end_headers() # 响应头结束 self.wfile.write('<h1>这是来自服务器的请求</h1>'.encode('utf-8')) # 响应数据体
def setUserSessionAndCookies(self, request, userj, cookies={}, backend="memcached" ): if self._mc == None: raise ValueError("setUserSession called without memcached being setup") skey = uuid.uuid4().hex # Send the session cookie back to the user c = http.cookies.SimpleCookie() c['mrsession'] = skey c['mrsession']['path'] = '/' #TODO c['mrsession']['expires'] = 12 * 30 * 24 * 60 * 60 # 1 year TODO arg for k in cookies.keys(): c[k] = cookies[k] c[k]['path'] = '/' c[k]['expires'] = 12 * 30 * 24 * 60 * 60 request.response.cookies = c self._mc.set( skey, userj )
def do_GET(self): #print(self.path) cookies = http.cookies.SimpleCookie(self.headers.get('Cookie')) ''' ## DEBUG ## sql="SHOW TABLES;" tables = mydb.mysql_query(sql) client.publish("coffee/debug", json.dumps(tables).encode(), qos=0, retain=False) s = [str(i) for i in tables] pom = ','.join(s) client.publish("coffee/debug", pom, qos=0, retain=False) ##################### ''' #print(cookies) if 'coffee_user' in cookies.keys(): url = urllib.parse.parse_qs( urllib.parse.urlparse(self.path).query ).get( 'api', None ) # https://stackoverflow.com/questions/8928730/processing-http-get-input-parameter-on-server-side-in-python if url == None: if self.path == "/": self.path = "/index.html" try: #Check the file extension required and #set the right mime type sendReply = False if self.path.endswith(".html"): mimetype = 'text/html' sendReply = True if self.path.endswith(".jpg"): mimetype = 'image/jpg' sendReply = True if self.path.endswith(".gif"): mimetype = 'image/gif' sendReply = True if self.path.endswith(".js"): mimetype = 'application/javascript' sendReply = True if self.path.endswith(".css"): mimetype = 'text/css' sendReply = True if self.path.endswith(".txt"): mimetype = 'text/plain' sendReply = True if sendReply == True: #Open the static file requested and send it f = open(curdir + sep + self.path, mode='rb') self.send_response(200) self.send_header('Content-type', mimetype) self.end_headers() self.wfile.write(f.read()) f.close() return except IOError: self.send_error(404, 'File Not Found: %s' % self.path) else: try: self._set_headers() if 'log' in url: self.wfile.write(json.dumps(show_log()).encode()) elif 'orders' in url: self.wfile.write( json.dumps(show_order_history_all()).encode()) elif 'orders_since_refill' in url: self.wfile.write( json.dumps( show_order_history_since_refill()).encode()) elif 'users' in url: self.wfile.write(json.dumps(show_users()).encode()) elif 'unregistered_users' in url: self.wfile.write( json.dumps(show_unregistered_users()).encode()) elif 'configure' in url: self.wfile.write(json.dumps(show_config()).encode()) else: self.wfile.write( json.dumps({ 'error': 'unknown_parameter' }).encode()) except BrokenPipeError: pass else: mimetype = 'text/html' self.send_response(200) self.send_header('Content-type', mimetype) self.end_headers() html = """<form action="" method="post">Password: <input type="password" name="password" required><input type="submit"></form>""" self.wfile.write(bytes(html, 'utf-8'))