gpt4 book ai didi

python - Python 中的 header 检查 (GAE)

转载 作者:行者123 更新时间:2023-12-01 05:51:03 24 4
gpt4 key购买 nike

我想知道如何检查 HTTP header 以确定请求是有效还是格式错误。我如何在 Python 中执行此操作,更具体地说,如何在 GAE 中执行此操作?

最佳答案

对于一些调试和查看带有 header 的请求,我使用以下 DDTHandler 类。

import cgi
import wsgiref.handlers
import webapp2

class DDTHandler(webapp2.RequestHandler):

def __start_display(self):
self.response.out.write("<!--\n")

def __end_display(self):
self.response.out.write("-->\n")

def __show_dictionary_items(self,dictionary,title):
if (len(dictionary) > 0):
request = self.request
out = self.response.out
out.write("\n" + title + ":\n")
for key, value in dictionary.iteritems():
out.write(key + " = " + value + "\n")

def __show_request_members(self):
request = self.request
out = self.response.out
out.write(request.url+"\n")
out.write("Query = "+request.query_string+"\n")
out.write("Remote = "+request.remote_addr+"\n")
out.write("Path = "+request.path+"\n\n")
out.write("Request payload:\n")
if (len(request.arguments()) > 0):
for argument in request.arguments():
value = cgi.escape(request.get(argument))
out.write(argument+" = "+value+"\n")
else:
out.write("Empty\n")

self.__show_dictionary_items(request.headers, "Headers")
self.__show_dictionary_items(request.cookies, "Cookies")

def view_request(self):
self.__start_display()
self.__show_request_members()
self.__end_display()

def view(self, aString):
self.__start_display()
self.response.out.write(aString+"\n")
self.__end_display()

示例:

class RootPage(DDTHandler):

def get(self):
self.view_request()

将输出请求并包含 header

所以检查代码并得到你需要的东西。如上所述,格式错误的“无效”请求可​​能不会到达您的应用程序。

<!--
http://localhost:8081/
Query =
Remote = 127.0.0.1
Path = /

Request payload:
Empty

Headers:
Referer = http://localhost:8081/_ah/login?continue=http%3A//localhost%3A8081/
Accept-Charset = ISO-8859-7,utf-8;q=0.7,*;q=0.3
Cookie = hl=en_US; dev_appserver_login="test@example.com:False:185804764220139124118"
User-Agent = Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17
Host = localhost:8081
Accept = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language = en-US,en;q=0.8,el;q=0.6

Cookies:
dev_appserver_login = test@example.com:False:185804764220139124118
hl = en_US
-->

关于python - Python 中的 header 检查 (GAE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14305974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com