Python 重定向 响应头

来源:互联网 发布:在淘宝上搜春药怎么搜 编辑:程序博客网 时间:2024/05/29 11:44
请求头:GET /articles/2001/ HTTP/1.1Host: 192.168.137.3:9000User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2Accept-Encoding: gzip, deflateCookie: csrftoken=YbGeEfNIf2Hcl6TF5wdi2WhvJ1JMGvmuBfH91cNsJLC4WO3TFCv84O0spWsZqXeT; sessionid=besfz007xaeknwdgt1lr6mm4dcib4oz2Connection: keep-aliveUpgrade-Insecure-Requests: 1响应头:HTTP/1.0 302 FoundDate: Mon, 27 Nov 2017 10:01:37 GMTServer: WSGIServer/0.1 Python/2.7.3X-Frame-Options: SAMEORIGINContent-Type: text/html; charset=utf-8Location: http://www.baidu.com/Content-Length: 0http://192.168.137.3:9000/articles/2017/curl "http://192.168.137.3:9000/articles/2017/" --1.0 -H "Host: 192.168.137.3:9000" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2" --compressed -H "Cookie: csrftoken=YbGeEfNIf2Hcl6TF5wdi2WhvJ1JMGvmuBfH91cNsJLC4WO3TFCv84O0spWsZqXeT; sessionid=besfz007xaeknwdgt1lr6mm4dcib4oz2" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1"此时有了302 重定向python 获取重定向信息,重定向信息位于响应头import httplibdef check_web_server(host,port,path):    h=httplib.HTTPConnection(host,port)    h.request('GET',path)    print h.host    print h.port        resp = h.getresponse()    print  'status  =',  resp.status    print  'reason  =',  resp.reason    print 'HTTP Headers:'    for hdr in resp.getheaders():        print  ' %s: %s' % hdrcheck_web_server('192.168.137.3','9000','/articles/2017/')C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p12.py192.168.137.39000status  = 302reason  = FoundHTTP Headers: content-length: 0 server: WSGIServer/0.1 Python/2.7.3 location: http://www.baidu.com/ date: Mon, 27 Nov 2017 11:06:54 GMT x-frame-options: SAMEORIGIN content-type: text/html; charset=utf-8node2:/django/mysite/news#cat views.py# -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom django.shortcuts import renderfrom django.http import HttpResponse# Create your views here.from django.shortcuts import render,render_to_response,redirectfrom .models import Articlefrom django.http import HttpResponse, HttpResponseNotFoundimport datetimefrom django.views.decorators.http import require_http_methods,require_GETfrom django.http import HttpResponseRedirectfrom django.shortcuts import renderimport osdef year_archive(request, year):     print '-------------------------'     print year     print '-------------------------'     response = "You're looking at first  the results of question %s."     #return HttpResponse(response % year)     return redirect('http://www.baidu.com/')