[英]Python— Extracting http headers
req = urllib2.Request
var1 = req.get_header('var1', default=None)
logging.info(var1)
var2 = req.get_header('var2', default=None)
logging.info(var2)
var3= req.get_header('var3', default=None)
logging.info(var3)
var4 = req.get_header('var4', default=None)
logging.info(var4)
編寫了上述python腳本以提取http標頭,但是在Google App Engine上運行時出現以下錯誤。
<type 'exceptions.TypeError'>: unbound method get_header() must be called with Request instance as first argument (got str instance instead)
Traceback (most recent call last):
File "/base/data/home/apps/s~##############/##########/@@@@@@@@@.py", line 10, in <module>
var1= req.get_header('var1', default=None)
適用於App Engine的Python開發的新手,因此無法找出錯誤。
您似乎在做一些非常奇怪的事情:您正在從處理程序向服務於同一處理程序的URL發出Web請求,然后嘗試獲取您實際發出的請求的標頭。 你為什么要這樣做? 這根本沒有意義。
我認為您真正想要做的是從已經傳遞給處理程序的請求對象中獲取標頭,當然可以通過self.request
。
你可以這樣
>>> req = urllib2.Request("http://www.google.com")
>>> response = urllib2.urlopen(req)
>>> response.info().getheader('Content-Type')
'text/html; charset=ISO-8859-9'
>>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.