一、request提供了一个一个叫做session的类,来实现客户端和服务端的会话保持

# coding:utf-8
import requests
url = "https://passport.cnblogs.com/user/signin"

headers = {
                  头部信息已省略

                   }
payload = {"input1":"xxx",
                "input2":"xxx",
                "remember":True}

s = requests.session()
r = s.post(url, json=payload, headers=headers,verify=False)
print r.json()

二、获取登录后的cookie 和 token

请求成功之后获取cookies,然后吧cookies存到固定配置文件里面

    def post(self):
        try:
            r = requests.post(self.url, headers=self.headers, params=self.params, data=self.data, timeout=float(timeout))
            # response.raise_for_status()
            result =decodeToBase64(r.content)
            response = json.loads(result)
            cookies = r.cookies.get_dict()   #  获取登录之后 cookie
            localReadConfig.set_headers("cookies", str(cookies))   #把cookie存到配置文件里面去
            return response
        except TimeoutError:
            self.logger.error("Time out!")
            return None

获取登录后的token

def login():
    """
    login
    :return: token
    """
    # set url
    url = common.get_url_from_xml('login')
    localConfigHttp.set_url(url)

    # set header
    token = localReadConfig.get_headers("token_v")
    header = {"token": token}
    localConfigHttp.set_headers(header)

    # set param
    data = {"email": localLogin_xls[0][3],
            "password": localLogin_xls[0][4]}
    localConfigHttp.set_data(data)

    # login
    response = localConfigHttp.post().json()
    token = common.get_value_from_return_json(response, "member", "token")
    return token

保持登录的接口请求,就获取cookie然后传到头部header里面去,token传到body里面去,然后就可以发布成功了。