如何使用python迭代webapp RequestHandler中的所有请求头?

J.O*_*sen 5 python google-app-engine

我需要遍历所有请求标头对象并在App Engine中打印它.尝试使用循环时出错.如何正确地做到这一点?

class MainHandler(webapp.RequestHandler):
    def get(self):
        for e in self.request.headers:
            self.request.headers(e + "<br />")
Run Code Online (Sandbox Code Playgroud)

我收到错误: AttributeError: EnvironHeaders instance has no __call__ method

Nil*_*esh 8

错误self.request.headers(e + "<br />")符合要求.您正在尝试调用该request.headers方法.

我检查联机帮助,发现self.request.headersdict喜欢的对象.您可以登录https://developers.google.com/appengine/docs/python/gettingstarted/usingwebapp

迭代headers你可以使用self.request.headers.items()self.request.headers.keys()