我在python 2.7中使用龍捲風4.3。我想重定向一個請求到另一個one.For例如,當我張貼http://127.0.0.1/,我想發佈到https://myip.ipip.net/.This可以使用nginx的CONF下面龍捲風怎麼能set_status 307
return 307 $scheme://myip.ipip.net$request_uri;
做但現在的目標URL並不總是相同的,所以我無法在nginx中硬編碼。 由於我的服務器是龍捲風,我應該使用代碼來工作。 正如我們所知,龍捲風支持使用self.set_status(HTTP_CODE)
設置HTTP代碼。 但是,當我設置307它響應302到客戶端。怎麼了?
這裏是我的代碼
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def post(self):
self.set_status(307)
self.redirect("https://myip.ipip.net/") #
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
這是我與curl.Tornado測試responsed 302雖然我設置307
curl -LI 127.0.0.1:8888 -XPOST
結果如下
HTTP/1.1 302 Found
Date: Wed, 10 May 2017 07:31:17 GMT
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Location: https://myip.ipip.net/
Server: TornadoServer/4.3
HTTP/1.1 200 OK
Server: NewDefend
Date: Wed, 10 May 2017 07:31:18 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 69
Connection: keep-alive
X-Cache: from ctl-zj-122-228-198-138
謝謝你!
我發現原因在源代碼'self.redirect(URL,永久=假,狀態=無)',默認狀態爲302或301 ......,所以設置狀態在self.redirect可以做.. –