• Python 重定向 响应头


    请求头:
    
    GET /articles/2001/ HTTP/1.1
    Host: 192.168.137.3:9000
    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    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
    Accept-Encoding: gzip, deflate
    Cookie: csrftoken=YbGeEfNIf2Hcl6TF5wdi2WhvJ1JMGvmuBfH91cNsJLC4WO3TFCv84O0spWsZqXeT; sessionid=besfz007xaeknwdgt1lr6mm4dcib4oz2
    Connection: keep-alive
    Upgrade-Insecure-Requests: 1
    
    
    响应头:
    
    HTTP/1.0 302 Found
    Date: Mon, 27 Nov 2017 10:01:37 GMT
    Server: WSGIServer/0.1 Python/2.7.3
    X-Frame-Options: SAMEORIGIN
    Content-Type: text/html; charset=utf-8
    Location: http://www.baidu.com/
    Content-Length: 0
    
    
    http://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 httplib
    def 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' % hdr
    check_web_server('192.168.137.3','9000','/articles/2017/')
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p12.py
    192.168.137.3
    9000
    status  = 302
    reason  = Found
    HTTP 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-8
    
    
    
    node2:/django/mysite/news#cat views.py
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    
    from django.shortcuts import render
    from django.http import HttpResponse
    
    # Create your views here.
    from django.shortcuts import render,render_to_response,redirect
    from .models import Article
    from django.http import HttpResponse, HttpResponseNotFound
    import datetime
    from django.views.decorators.http import require_http_methods,require_GET
    from django.http import HttpResponseRedirect
    from django.shortcuts import render
    import os
    def 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/')

  • 相关阅读:
    一、NHibernate 使用配置与实现简单查询
    NHibernate学习步骤
    sql2008转成2005的解决办法
    二、使用NHibernate对数据库实现增、删、改、查操作
    FusionCharts 点击事件 新页面
    [分享]NHibernate视频教程
    FusionCharts Free使用文档教程第五章FusionCharts Free使用dataXML加载数据
    职业规划
    网页生成pdf文件
    FusionCharts Free使用文档教程第四章FusionCharts Free使用JavaScript加载图形
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349406.html
Copyright © 2020-2023  润新知