zoukankan      html  css  js  c++  java
  • Django 模拟登陆,验证接口可用行

    import requests

    LOGIN_URL = "http://mydjangosite.com/accounts/login/"
    ENDPOINT_URL = 'http://mydjangosite.com/myendpoint/'

    '''
    Create a session.
    A session will automatically store the cookies that Django
    sends back to you, like the csrf token and a the session id. You
    could do it without the session, but then you'd have to save off the
    cookies manually and remember to pass them along with subsequent
    requests.
    '''
    client = requests.session()
    client.get(LOGIN_URL)

    import requests

    LOGIN_URL = "http://mydjangosite.com/accounts/login/"
    ENDPOINT_URL = 'http://mydjangosite.com/myendpoint/'

    '''
    Create a session.
    A session will automatically store the cookies that Django
    sends back to you, like the csrf token and a the session id. You
    could do it without the session, but then you'd have to save off the
    cookies manually and remember to pass them along with subsequent
    requests.
    '''
    client = requests.session()
    client.get(LOGIN_URL)
    # Django would like the csrf token passed with the data, so we do need to save it off seperately.
    csrftoken = client.cookies['csrftoken']

    '''
    Log in.
    '''
    login_data = {login:"somepersonsname", password:"supergreatpassword", csrfmiddlewaretoken:csrftoken}
    r1 = client.post(LOGIN_URL, data=login_data)
    # For some reason, we are issued a new csrf token after logging in, so update your local copy.
    csrftoken = client.cookies['csrftoken']

    '''
    Post some data to your login-only API endpoint.
    '''
    payload = {'somedata':'asdf', 'someotherdata':'1235', 'csrfmiddlewaretoken':csrftoken}
    # We use client.post (not requests.post) so that we pass on the cookies that our session stored.
    r2 = client.post(ENDPOINT_URL, data=payload)


    # Django would like the csrf token passed with the data, so we do need to save it off seperately.
    csrftoken = client.cookies['csrftoken']

    '''
    Log in.
    '''
    login_data = {login:"somepersonsname", password:"supergreatpassword", csrfmiddlewaretoken:csrftoken}
    r1 = client.post(LOGIN_URL, data=login_data)
    # For some reason, we are issued a new csrf token after logging in, so update your local copy.
    csrftoken = client.cookies['csrftoken']

    '''
    Post some data to your login-only API endpoint.
    '''
    payload = {'somedata':'asdf', 'someotherdata':'1235', 'csrfmiddlewaretoken':csrftoken}
    # We use client.post (not requests.post) so that we pass on the cookies that our session stored

    headers = {"Content-Type": "application/json", "accept": "application/json",
    "X-CSRFToken": csrftoken}

    result = client.post(endpoint_url, json=payload, headers=headers)

     
    本博客的内容如果没有标注转载字样,均属个人原创!欢迎学习交流,如果觉得有价值,欢迎转载,转载请注明出处,谢谢!
  • 相关阅读:
    【嵌入式硬件Esp32】Ubuntu18.04 更换阿里云软件源
    【嵌入式硬件Esp32】Ubuntu 1804下ESP32交叉编译环境搭建
    【嵌入式硬件Esp32】Eclipse c++切换回英文方法
    Ant Design使用问题记录
    C#调用python脚本
    C# 6新特性简单总结
    ASP.NET动态网站制作(30)-- WEBService
    ASP.NET动态网站制作(29)-- 正则
    ASP.NET动态网站制作(28)-- 三层框架(2)
    ASP.NET动态网站制作(27)-- 三层框架(1)
  • 原文地址:https://www.cnblogs.com/L-O-N/p/12916726.html
Copyright © 2011-2022 走看看